angularjs - Using angular variables inside non-angular attributes -
something not understanding angular consistency behind usage of braces.
say want pass in angular var ng-click function:
ng-click="getcomments('{{post.id}}')"
this looks ok in html. when click, parameter still '{{post.id}}' though in html code replaced angular.
also want this:
<customdirective customattribute="{{post.id}}_postid" ></customdirective>
this throws parsing error , don't know why. here error:
syntax error: token 'post.id' unexpected, expecting [:] @ column 3 of expression [{post.id}}_postid] starting @ [post.id}}_postid].
all above code within ng-repeat.
in case of ng-click=""
whatever passed between quotation marks code, here variables defined $scope.variable
available variable
, working code
ng-click="getcomments(post.id)"
as second part, when writing own directive specify attributes passed so:
scope: { attra: '@', attrb: '=' }
attra
bind string while attrb
bind object, or whatever type variable may be. details see so answer ensure using right binding use-case.
so second code sample should read:
<customdirective customattribute="{{post.id}}+'_postid'" ></customdirective>
as text inside brackets should seen code used when defining other js variable.
as whole conundrum of when use braces in angularjs, here great so answer on topic.
edit:
i have tried out alternatives directive problem, 1 works doesn't update after value changes if matters:
<customdirective customattribute="post.id+'_postid'" ></customdirective>
though suggest approaching problem wrong solution, given aren't interested in figuring out angular's binding system.
the best solution edit string in teh directive, or bind object referencing function ensure changes propagated:
$scope.post = function(){ return { id: $scope.somevar.id + "_id" }; };
Comments
Post a Comment