AngularJS - 单击所有项目的重复内部
AngularJS - Ng click inside ng repeat for all items
我有以下 AngularJS 脚本:
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="module.changer = { 'on': 'off', 'off':'on'}[module.changer]; event.PreventDefault;">{{module.changer}} </span></li>
</ul></div>
也请看看下面的Plunker:
https://plnkr.co/edit/a7aTK09lDJ3FlCHgdWIf?p=preview
当我现在点击一个项目 'changer'(打开或关闭)时,它将切换到特定项目的另一个值。
但我希望当我单击其中一项时,ng-repeat 中所有项目的值都在改变。我该怎么做?
试试这个,http://plnkr.co/edit/BFqR1PMsCKugIcLMGake?p=preview
HTML
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9" data-require="angular.js@1.4.x"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="changeValue()">{{module.changer}} </span></li>
</ul>
</div>
</body>
</html>
JAVASCRIPT
(函数() {
'use strict';
angular
.module('app', [])
.controller('myctrl', myctrl);
function myctrl($scope, $http) {
$http.get("data.json")
.success(function(data) {
$scope.data = data;
})
.error(function(data, status) {
console.error('Repos error', status, data);
});
$scope.changeValue = function() {
//module.changer = { 'on': 'off', 'off':'on'}[module.changer]; event.PreventDefault;
for (var dat in $scope.data.data) {
$scope.data.data[dat].changer = ($scope.data.data[dat].changer == 'on') ? 'off' : 'on';
}
}
}
您好,据我了解您的问题,您可以通过定义一个函数来实现,该函数将 json 中的所有值设置为 changer 字段
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="setValue()">
{{module.changer}} </span></li>
</ul>
</div>
并且在控制器中...
$scope.setValue=function(){
$scope.data.data.forEach(function(it){
it.changer=(it.changer=='off'? 'on':'off');
event.PreventDefault;
})
}
我有以下 AngularJS 脚本:
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="module.changer = { 'on': 'off', 'off':'on'}[module.changer]; event.PreventDefault;">{{module.changer}} </span></li>
</ul></div>
也请看看下面的Plunker: https://plnkr.co/edit/a7aTK09lDJ3FlCHgdWIf?p=preview
当我现在点击一个项目 'changer'(打开或关闭)时,它将切换到特定项目的另一个值。
但我希望当我单击其中一项时,ng-repeat 中所有项目的值都在改变。我该怎么做?
试试这个,http://plnkr.co/edit/BFqR1PMsCKugIcLMGake?p=preview
HTML
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9" data-require="angular.js@1.4.x"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="changeValue()">{{module.changer}} </span></li>
</ul>
</div>
</body>
</html>
JAVASCRIPT
(函数() { 'use strict'; angular .module('app', []) .controller('myctrl', myctrl);
function myctrl($scope, $http) {
$http.get("data.json")
.success(function(data) {
$scope.data = data;
})
.error(function(data, status) {
console.error('Repos error', status, data);
});
$scope.changeValue = function() {
//module.changer = { 'on': 'off', 'off':'on'}[module.changer]; event.PreventDefault;
for (var dat in $scope.data.data) {
$scope.data.data[dat].changer = ($scope.data.data[dat].changer == 'on') ? 'off' : 'on';
}
}
}
您好,据我了解您的问题,您可以通过定义一个函数来实现,该函数将 json 中的所有值设置为 changer 字段
<div ng-app='app' ng-controller="myctrl">
<ul ng-repeat="module in data.data">
<li >{{module.name}}<br><span ng-click="setValue()">
{{module.changer}} </span></li>
</ul>
</div>
并且在控制器中...
$scope.setValue=function(){
$scope.data.data.forEach(function(it){
it.changer=(it.changer=='off'? 'on':'off');
event.PreventDefault;
})
}