【发布时间】:2015-11-09 10:13:07
【问题描述】:
$scope.new 是一个变量..我试图在 ng-repeat 中访问它,但 ng-repeat 将“new”视为局部变量并仅在其范围内更改其值...我想给出具体的$scope.new 变量的索引...不使用函数...谢谢....
<html>
<style>
.yoyo{font-size:30px;}
</style>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
$scope.new=0;
});
</script>
<body>
<div data-ng-app="myApp" ng-controller="customersCtrl" data-ng-init="names=['Jani','Hege','Kai']">
<p>Looping with ng-repeat:</p>
<ul>
<li data-ng-repeat="x in names" ng-click="new=$index" ng-class="yoyo:new==$index">
{{ x }}{{$index}}{{new}}
</li>
</ul>
</body>
</html>
【问题讨论】:
-
当您执行“new=$index”时,控制器中的“new”也会更新。你到底想达到什么目标?
-
使用这个::
ng-click="$parent.new=$index" -
让我知道它是否有效?@shank???
-
非常感谢罗伊...它工作正常...但是 $parent 如何访问 $scope ?
标签: angularjs angularjs-ng-repeat