我有以下按钮,我用作切换。
<button ng-click="togglefunction()">Toggle Data</button>
这是应该工作的切换部分
$scope.toggleToolPanel = function () {
// need to put below 2 functions here so that when user clicks 1st time, function 1 executes. when user clicks again, function 2 executes and so on.
};
这两个函数应该在toggleFunction
中交替执行 function function1(params) {
return '<span >' + data + '</span>';
}
function function2(params) {
return '<span >' + data *100 + '</span>';
}
答案 0 :(得分:2)
将此添加到您的控制器:
$scope.firstFunction = false;
然后将您的toggleToolPanel
更改为以下内容:
$scope.toggleToolPanel = function() {
$scope.firstFunction = !$scope.firstFunction;
if($scope.firstFunction) {
function1(params);
} else {
function2(params);
}
};
答案 1 :(得分:1)
每次点击按钮元素时,都会切换一个类。见classList.toggle
。在单击事件处理程序中,使用classList.contains查找切换的存在。如果有x,如果不做y。
答案 2 :(得分:1)
清洁代码附于下方:
angular.module('mainModule', [])
.controller('MainCtrl', ['$scope', function($scope) {
$scope.toggle = function() {
$scope.isToggled = !$scope.isToggled;
var params = $scope.isToggled;
$scope.isToggled ? toggleIn(params) : toggleOut(params);
};
function toggleIn(params) {
console.log(params);
}
function toggleOut(params) {
console.log(params);
}
}]);
<body ng-app="mainModule">
<div ng-controller="MainCtrl">
<input type="button" value="Toggle" ng-click="toggle()" />
</div>
</body>