如何到达ng-repeat范围之外的变量
[英] How to reach variable outside the ng-repeat scope
本文介绍了如何到达ng-repeat范围之外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以访问 ng-repeat 范围之外的变量?
jsfiddle:http://jsfiddle.net/zcbhubrw/
代码如下:
HTML
<div class="slide" ><div class="容器"><h2 class="section-title">选定的手机</h2>
<div class="container-fluid fix ver2"><div class="col-md-3 work-thumb" ng-repeat="phone in phone"><a href="#" ng-click="count = {{$index}}">{{电话名称}}</a>
<p>计数:{{count}}</p>
控制器
var phonecatApp = angular.module('phonecatApp', []);phonecatApp.controller('PhoneListCtrl', function ($scope) {$scope.phones = [{'name': 'Nexus S','snippet': '使用 Nexus S 使速度更快.'},{'name': 'Motorola XOOM™ with Wi-Fi','snippet': '下一代平板电脑.'},{'name': '摩托罗拉 XOOM™','snippet': '下一代平板电脑.'}];});
我需要访问 {{count}} 变量以供进一步使用.
解决方案
你可以像这样点击 ng-repeat 中的列表同时调用 函数
并在控制器中定义函数
$scope.setCount = function(index) {$scope.count = 索引;};
并在您点击时更新计数值
这是您的代码的工作plunker,
http://embed.plnkr.co/rAbanmAR2Chw3P501tDc/preview
希望这有帮助!
Is possible to reach variable outside the ng-repeat scope?
jsfiddle:
http://jsfiddle.net/zcbhubrw/
Here is the code:
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>
Controller
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.'}
];
});
I need to reach the {{count}} variable for further usage.
解决方案
you can call a function while clicking the list inside ng-repeat
like this
<a href="#" ng-click="setCount($index)">
and defining the function in controller
$scope.setCount = function(index) {
$scope.count = index;
} ;
and updating the count value when you click
Here is a working plunker with your code,
http://embed.plnkr.co/rAbanmAR2Chw3P501tDc/preview
Hope this helps!
这篇关于如何到达ng-repeat范围之外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!