是否可以在ng-repeat范围之外达到变量?
的jsfiddle: http://jsfiddle.net/zcbhubrw/
以下是代码:
HTML
<div class="section" ng-app="phonecatApp" ng-controller="PhoneListCtrl">
<div class="slide" >
<div class="container">
<h2 class="section-title">Selected Mobiles</h2>
</div>
<div class="container-fluid fix ver2">
<div class="col-md-3 work-thumb" ng-repeat="phone in phones">
<a href="#" ng-click="count = {{$index}}">
{{phone.name}}
</a>
</div>
</div>
</div>
<p>Count: {{count}}</p>
</div>
控制器
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope) {
$scope.phones = [
{'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.'},
{'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.'},
{'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.'}
];
});
我需要访问 {{count}} 变量以供进一步使用。
答案 0 :(得分:1)
您可以在控制器中创建功能以设置计数或从ng-repeat
中引用父范围ng-click="$parent.count = $index"
答案 1 :(得分:1)
你可以像这样打电话给function while clicking the list inside ng-repeat
<a href="#" ng-click="setCount($index)">
并在控制器中定义函数
$scope.setCount = function(index) {
$scope.count = index;
} ;
并在单击
时更新计数值这是一个包含代码的工作人员,
http://embed.plnkr.co/rAbanmAR2Chw3P501tDc/preview
希望这有帮助!