简体   繁体   English

从angularjs 1.0.8升级到1.2.3后,ng-repeat无法正常工作

[英]ng-repeat not working after upgrading from angularjs 1.0.8 to 1.2.3

I recently upgraded from angularjs 1.0.8 to 1.2.3 and it has been a nightmare. 我最近从angularjs 1.0.8升级到1.2.3,这是一场噩梦。 So many things have broken, some of which I have been able to figure out. 许多事情已经破裂,其中一些我已经能够弄清楚。 There is a problem with ng-repeat that I have been unable to fix. ng-repeat存在问题我无法修复。 When using 1.0.8 the following code worked as expected: 使用1.0.8时,以下代码按预期工作:

<ul id="my_list">
    <li ng-click="doSomething('{{item.item_id}}')" ng-repeat="item in items">
        <span>This is item {{item.item_id}}</span>
    </li>
</ul>

However, now that I have upgraded to 1.2.3 each <li> will display the item_id in the <span> (ie, "This is item 1", "This is item 2", etc.), but the function still just displays doSomething('{{item.item_id}}') as opposed to, for example, doSomething('1') . 但是,现在我已升级到1.2.3, <li>将在<span>显示item_id (即“这是第1项”,“这是第2项”等),但功能仍然只是显示doSomething('{{item.item_id}}') ,而不是例如doSomething('1') Why is {{item.item_id}} binding in one place, but not the other? 为什么{{item.item_id}}绑定在一个地方,而不是另一个?

ng-click采用JS表达式,因此<li ng-click="doSomething(item.item_id)" ng-repeat="item in items">应该有效。

ngClick doesn't interpolate the expression. ngClick不会插入表达式。 Use doSomething(item.item_id) instead. 请改用doSomething(item.item_id)

As Jeff and ceejayoz point out ng-click expects an Angular expression, so you can access scope properties without binding to them. 正如Jeff和ceejayoz指出ng-click需要一个Angular表达式,因此您可以访问范围属性而不绑定它们。

Use this if you need the parameter as a string: 如果您需要将参数作为字符串,请使用此选项:

ng-click="doSomething(item.item_id.toString())"

For adding the value in the DOM you need the curly braces, which tell Angular to expect an expression like ng-click does. 要在DOM中添加值,您需要花括号,它告诉Angular期望像ng-click这样的表达式。

I figured it out. 我想到了。 It was because I wrote the function with a semicolon as the end: doSomething('{{item.item_id}}'); 这是因为我用分号作为结尾编写了函数: doSomething('{{item.item_id}}'); . I found that onclick="doSomething('{{item.item_id}}')" doesn't work, but ng-click="doSomething('{{item.item_id}}')" does work. 我发现onclick="doSomething('{{item.item_id}}')"不起作用,但ng-click="doSomething('{{item.item_id}}')"确实有效。

As a side note: I have been working with angularjs for about 5 months now, and I have to say that it has led to an increase in the complexity of my apps and it is much more complicated that just using jquery. 作为旁注:我现在已经使用angularjs大约5个月了,我不得不说它导致我的应用程序的复杂性增加,而且使用jquery会更复杂。 I know many people will disagree, but it just seems like it is MUCH more trouble than its worth. 我知道很多人会不同意,但似乎它比它的价值更麻烦。 I kind of regret using it. 我有点后悔使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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