AngularJS从外部访问作用域ng-repeat
AngularJS access scope from outside ng-repeat
我想显示在我的ng-repeat中重复的项目计数,但我如何从ng-repeat外部访问它?
恰好: http://plnkr.co/edit/SYqb8h9k81SlVY3Yzhgx
HTML:<h3>Number of locations is: {{location.length}}</h3>
<div ng-controller="locationAccordionCtrl">
<div ng-repeat="location in locations" on-finish-render>
{{location.siteName}}
<ul>
<li ng-repeat="listOfLocations in location.locationsList track by $index">
{{listOfLocations}}
</li>
</ul>
</div>
</div>
JS:
var App = angular.module('App', []);
App.controller('locationAccordionCtrl', function ($scope) {
$scope.locations = [
{
"siteName":"First Location",
"locationsList":['First Location 1', 'First Location 2', 'First Location 3']
},
{
"siteName":"Second Location",
"locationsList":['Second Location 1', 'Second Location 2', 'Second Location 3']
},
{
"siteName":"Third Location",
"locationsList":['Third Location 1', 'Third Location 2', 'Third Location 3']
}
];
});
<h3>
标签必须在locationAccordionCtrl
的范围内。因此,将其放入<div ng-controller="locationAccordionCtrl">
标签中。此外,您必须说{{locations.length}}
而不是{{location.length}}
(缺少"s")
问题不在于它在ng-repeat
之外,而在于它在控制器之外。下面的代码应该可以工作:
<div ng-controller="locationAccordionCtrl">
<h3>Number of locations is: {{locations.length}}</h3>
[...]
把绑定放到控制器中:
<div ng-controller="locationAccordionCtrl">
<h3>Number of locations is: {{locations.length}}</h3>
<div ng-repeat="location in locations" on-finish-render>
{{location.siteName}}
<ul>
<li ng-repeat="listOfLocations in location.locationsList track by $index">
{{listOfLocations}}
</li>
</ul>
</div>
</div>
<div ng-controller="locationAccordionCtrl">
<h3>Number of locations is: {{locations.length}}</h3>
<div ng-repeat="location in locations" on-finish-render>
{{location.siteName}} ({{location.locationsList.length}})
<ul>
<li ng-repeat="listOfLocations in location.locationsList track by $index">
{{listOfLocations}}
</li>
</ul>
</div>
</div>
相关文章:
- ng根据作用域值隐藏
- 角度指示ng重复作用域和服务
- 以新模式访问作用域形式ng repeat
- AngularJS:将ng重复作用域暴露给ng transclude
- 使用ng repeat中的键值来扩展作用域表达式
- 如何在使用ng repeat时停止传播(或者如何更好地隔离作用域)
- 角度指令作用域绑定在 ng-repeat中不起作用
- 如何在具有隔离作用域的指令中使用 ng-repeat
- 如何使用 ng-repeat引用作用域中的特定变量
- 将作用域变量设置为ng repeat中的ng模型
- AngularJS的ng-include作用域
- 使用ng-click在与ng-repeat相同的标签上使用ng-repeat作用域的参数
- Angularjs如何访问ng-repeat作用域外的过滤值
- 如何从两次或三次嵌套的ng-repeat作用域访问控制器的作用域
- 使用ng-options时,Ng-model作用域不可用
- 改变函数中的ng-repeat作用域不会更新作用域
- 访问ng重复作用域
- 如何在ng重复作用域内创建属性
- ng-repeat作用域的奇怪行为
- 如何访问ng-repeat作用域?