Angular ng-click 在 ng-repeat 中不起作用
Angular ng-click not working in ng-repeat
我试图在 ng-repeat 中使用 ng-click 将数组元素保存在另一个变量中,但是当我单击该元素时,它不起作用。
这是我的代码:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="myCtrl">
<div ng-repeat="img in images">
<a ng-click="showing = img">{{img.descricao}}</a>
</div>
--------------------
Clicked element description is: {{showing.descricao}}
</body>
</html>
这是我的 javascript:
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.images = [
{
"id": 1,
"image": "http://placehold.it/300x300",
"ordem": 1,
"descricao": "Descricao 1"
},
{
"id": 2,
"image": "http://placehold.it/300x300",
"ordem": 2,
"descricao": "Descricao 2"
}
];
$scope.showing = {};
});
这是我的完整代码:http://jsbin.com/fuyuci/1/edit
我哪里错了?
我认为您在更改 ng-repeat 中显示的值时使用不正确,因为 ng-repeat 创建自己的范围。相反,您应该绑定一个函数来为 $scope.showing 设置值。
例如:
$scope.changeShowing = function(img) {
`$scope.showing = img;`
};
ng-click ="changeShowing(img)"'
我试图在 ng-repeat 中使用 ng-click 将数组元素保存在另一个变量中,但是当我单击该元素时,它不起作用。
这是我的代码:
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="myCtrl">
<div ng-repeat="img in images">
<a ng-click="showing = img">{{img.descricao}}</a>
</div>
--------------------
Clicked element description is: {{showing.descricao}}
</body>
</html>
这是我的 javascript:
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.images = [
{
"id": 1,
"image": "http://placehold.it/300x300",
"ordem": 1,
"descricao": "Descricao 1"
},
{
"id": 2,
"image": "http://placehold.it/300x300",
"ordem": 2,
"descricao": "Descricao 2"
}
];
$scope.showing = {};
});
这是我的完整代码:http://jsbin.com/fuyuci/1/edit
我哪里错了?
我认为您在更改 ng-repeat 中显示的值时使用不正确,因为 ng-repeat 创建自己的范围。相反,您应该绑定一个函数来为 $scope.showing 设置值。 例如:
$scope.changeShowing = function(img) {
`$scope.showing = img;`
};
ng-click ="changeShowing(img)"'