如何达到ng-repeat范围之外的变量 [英] How to reach variable outside the ng-repeat scope

查看:43
本文介绍了如何达到ng-repeat范围之外的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否达到ng-repeat范围之外的变量?

Is possible to reach variable outside the ng-repeat scope?

jsfiddle: http://jsfiddle.net/zcbhubrw/

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}} 变量以进一步使用.

I need to reach the {{count}} variable for further usage.

推荐答案

您可以像这样调用function while clicking the list inside ng-repeat

<a href="#" ng-click="setCount($index)">

并在控制器中定义功能

 $scope.setCount = function(index) {
    $scope.count = index;
} ;

并在单击时更新计数值

这是您代码中的工作对象,

Here is a working plunker with your code,

http://embed.plnkr.co/rAbanmAR2Chw3P501tDc/preview

希望这会有所帮助!

这篇关于如何达到ng-repeat范围之外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆