angularjs - Making isolated scope one-way data binding for expressions optional -


in directive created isolated scope binding expression attribute:

scope: {     foo: '@' } 

and in directive if try console log scope.foo, following function:

function (locals) {     return parentget(scope, locals); } 

and executing scope.foo() invoke expression in parent's scope. problem is, expression passing in optional callback, have no safe way of telling if attribute defined parent. function no matter expression attribute holds, if wasn't specified.

as result, testing existence of scope.foo doesn't help, , can't test scope.foo() because evaluate expression if there one. there way make expression binding optional? wish have similar '=?' when binding expressions, there doesn't seem '&?'.

this $parse comes in handy. delegates tasks of determining scope , if it's expression or constant. combining quick check see if attribute there so:

.directive('mydirective', function($parse){    return {       link: function($scope, elem, attrs){          if (attrs.foo){            var foogetter = $parse(attrs.foo)            var callback = foogetter($parse)            if (callback) callback.call()          }       }    } }) 

here docs on - $parse docs

but beef if doing kind of controller-as implementation combined callback, ofttimes javascript scope issues when using this . i'd prefer using optional scope callback why exists, calling methods on scope:

scope: { foo: '&?'} 

not sure version of angular you're using option had sought similar solution - optional two-way binding on isolate scope angular directive


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -