在 AngularJS 中使用一个函数控制两个 span 标签
Control two span tags using one function in AngularJS
我有两个 span 标签和两个 p 标签,例如:
<span class="span1" ng-click="show()">Span_1</span>
<p class="p1" ng-show="var1">P_1</p>
<span class="span2" ng-click="show()">Span_2</span>
<p class="p2" ng-show="var1">P_2</p>
我正在为此使用 AngularJS,所以我想要实现的是制作单个函数,例如上面显示的 show() 来切换两个 p 标签,我该如何实现?这是我的 JavaScript 代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.var1 = false;
$scope.show = function() {
$scope.var1 = $scope.var1 === false ? true: false;
};
});
点击 Span_1 后,只有与其对应的 p 标签显示与 Span_2 相同,我应该只通过一个函数实现。
这是 Plunker:http://plnkr.co/edit/fzgpV68Q7UnHGHmHffZZ?p=preview
您可以将跨度编号作为参数传递给函数,而不是基于参数更改 $scope.var1
或 $scope.var2
。功能会是什么样子?:
function show(varParam) {
if (varParam === 1) {
$scope.var1 = !$scope.var1; //flip value
} else {
$scope.var2 = !$scope.var2;
}
}
然后在模板中,您在函数调用期间传递参数,如下所示:
ng-click="show(1)"
传递来自 html 的变量。您还可以添加大小写,例如 ng-class 或等等等等。
//Angular 代码(js)
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.variableList = [{state:true},{state:false}];
$scope.show = function(what,where) {
switch(what){
case 'display':
return $scope.variableList[where].state;
break;
case 'click':
$scope.variableList[where].state = !$scope.variableList[where].state;
break;
case 'class':
return ($scope.variableList[where].state ? 'active' : '');
break;
}
};
});
用法(HTML):
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="item in variableList">
<span ng-class="show('class',$index);" ng-click="show('click',$index)">Span_{{$index+1}}</span>
<p ng-class="show('class',$index);" ng-show="show('display',$index)">P_{{$index+1}}</p>
</div>
</div>
你也可以编写切换span可见性的toggle方法。
例如 -
<html ng-app='myApp' >
<body ng-controller="myCtrl">
<span class="span1" ng-click="toggle()">Span_1</span>
<p class="p1" ng-show="var">P_1</p>
<span class="span2" ng-click="toggle()">Span_2</span>
<p class="p2" ng-show="!var">P_2</p>
</body>
</html>
JS -
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.var = true;
$scope.toggle = function() {
$scope.var = !$scope.var;
};
});
我有两个 span 标签和两个 p 标签,例如:
<span class="span1" ng-click="show()">Span_1</span>
<p class="p1" ng-show="var1">P_1</p>
<span class="span2" ng-click="show()">Span_2</span>
<p class="p2" ng-show="var1">P_2</p>
我正在为此使用 AngularJS,所以我想要实现的是制作单个函数,例如上面显示的 show() 来切换两个 p 标签,我该如何实现?这是我的 JavaScript 代码:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.var1 = false;
$scope.show = function() {
$scope.var1 = $scope.var1 === false ? true: false;
};
});
点击 Span_1 后,只有与其对应的 p 标签显示与 Span_2 相同,我应该只通过一个函数实现。
这是 Plunker:http://plnkr.co/edit/fzgpV68Q7UnHGHmHffZZ?p=preview
您可以将跨度编号作为参数传递给函数,而不是基于参数更改 $scope.var1
或 $scope.var2
。功能会是什么样子?:
function show(varParam) {
if (varParam === 1) {
$scope.var1 = !$scope.var1; //flip value
} else {
$scope.var2 = !$scope.var2;
}
}
然后在模板中,您在函数调用期间传递参数,如下所示:
ng-click="show(1)"
传递来自 html 的变量。您还可以添加大小写,例如 ng-class 或等等等等。
//Angular 代码(js)
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.variableList = [{state:true},{state:false}];
$scope.show = function(what,where) {
switch(what){
case 'display':
return $scope.variableList[where].state;
break;
case 'click':
$scope.variableList[where].state = !$scope.variableList[where].state;
break;
case 'class':
return ($scope.variableList[where].state ? 'active' : '');
break;
}
};
});
用法(HTML):
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="item in variableList">
<span ng-class="show('class',$index);" ng-click="show('click',$index)">Span_{{$index+1}}</span>
<p ng-class="show('class',$index);" ng-show="show('display',$index)">P_{{$index+1}}</p>
</div>
</div>
你也可以编写切换span可见性的toggle方法。 例如 -
<html ng-app='myApp' >
<body ng-controller="myCtrl">
<span class="span1" ng-click="toggle()">Span_1</span>
<p class="p1" ng-show="var">P_1</p>
<span class="span2" ng-click="toggle()">Span_2</span>
<p class="p2" ng-show="!var">P_2</p>
</body>
</html>
JS -
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.var = true;
$scope.toggle = function() {
$scope.var = !$scope.var;
};
});