angularjs - Non-assignable error in directive when using ternary operator -
i have directive has isolate scope action
property. when using ternary operator in view, i'm getting expression 'ctrl.someboolean ? 'some text' : 'different text'' used directive 'mydirective' non-assignable!
usages
<my-directive name='my name' action="ctrl.someboolean ? 'some text' : 'different text'"> <my-directive name='my name 2' action="'some static text'">
directive
angular.module('example') .directive('mydirective', [ function() { return { restrict: 'e', replace: true, transclude: true, templateurl: 'mydirective.html', scope: { name: '@', action: '=?' }, require: ['mydirective', 'form'], controller: 'mydirectivectrl', controlleras: 'ctrl', bindtocontroller: true }; }]);
directive controller
angular.module('example') .controller('mydirectivectrl', ['$scope', '$element', '$attrs', function($scope, $element, $attrs) { var vm = this; vm.action = angular.isdefined($attrs.action) ? $attrs.action : 'default action'; } ])
from way using directive, better not use two-way binding. need simple one-way attribute interpolation, i.e. directive usage should be:
<my-directive name='my name' action="{{ ctrl.someboolean ? 'some text' : 'different text' }}"></my-directive>
and directive scope configuration
scope: { name: '@', action: '@?' },
why angular throws error if use two-way binding ternary expression? easy understand if think two-way binding means. primary purpose of allow directive scope modify original outer scope value. however, not possible assign new value expression ctrl.someboolean ? 'some text' : 'different text'
. hence error.
Comments
Post a Comment