gpt4 book ai didi

javascript - 使用 $scope 来查看 this

转载 作者:行者123 更新时间:2023-12-03 09:00:27 25 4
gpt4 key购买 nike

var app = angular.module('plunker', []);
json = [1,2,3]
app.controller('MainCtrl', function($scope) {
$scope.edit = function(){
$scope.editMode = true;
};

$scope.data = json;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<li ng-click="edit()" ng-repeat="d in data">
{{d}}
<button ng-show="editMode == true">button</button>
</li>

</body>

</html>

当我点击指定的li时,所有按钮都会显示出来,如何只显示li内的按钮?我知道在 jquery 中你必须使用 $(this),但是在 angularjs 中怎么样?

最佳答案

由于您使用的是嵌入元素,因此 li 内的所有元素共享相同的范围。

您可以为数组中的每个元素设置编辑状态,例如

var app = angular.module('plunker', []);
json = [1, 2, 3]
app.controller('MainCtrl', function($scope) {
$scope.editMode = [];
$scope.edit = function(index) {
$scope.editMode[index] = true;
};

$scope.data = json;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<li ng-click="edit($index)" ng-repeat="d in data">
{{d}}
<button ng-show="editMode[$index]">button</button>
</li>

</body>

</html>

<小时/>

另一个选项是(如果您只想允许一次仅编辑 1 个元素)

var app = angular.module('plunker', []);
json = [1, 2, 3]
app.controller('MainCtrl', function($scope) {
$scope.editMode = -1;
$scope.edit = function(index) {
$scope.editMode = index;
};

$scope.data = json;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html ng-app="plunker">

<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.3/angular.js" data-semver="1.4.3"></script>
<script src="app.js"></script>
</head>

<body ng-controller="MainCtrl">
<li ng-click="edit($index)" ng-repeat="d in data">
{{d}}
<button ng-show="editMode == $index">button</button>
</li>

</body>

</html>

关于javascript - 使用 $scope 来查看 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com