简体   繁体   中英

Value not expanded in Angular templating

I try to understand the basics of angularJS and I test expanding template, because we have some issues with microtemplating we use before.

Please take a look to this fiddle

there is this line: <span ng-click="foo('{{item.id}}', '{{item.name}}')">{{item.name}}</span>

when I look via chrome inspector, it was expanded as expected: <span ng-click="foo('1', 'name1')" class="ng-binding">name1</span>

but the click alerts: {{item.id}} and {{item.name}} unexpanded.

where is the point I'm going wrong? What I have misunderstand?

in your foo function , you are passing strings. Try this <span ng-click="foo(item.id, item.name)">{{item.name}}</span>

:)

In Angular, interpolation is handled separately from directives, at a lower priority.

So what's happening is the ng-click directive is handled first; it parses "foo('{{item.id}}', '{{item.name}}')" without interpolation.
Then, the interpolation is handled, which updates the raw ng-click attribute to "foo('1', 'name1')" .

However, since ng-click already parsed the expression, it doesn't parse it again after interpolation changes. So that's why ng-click doesn't get the updated values.

As @Tedmosby said, the correct way to handle this behavior is to avoid using interpolation within a directive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM