在ng-click中指定多个动作的一种“正确”方法是用分号分隔每个动作,但是我遇到了在同一个标签上指定两个ng-click指令的代码。是否会执行两组ng-click指令,这会导致错误,还是只会执行一条或其他ng-click指令?
答案 0 :(得分:1)
如果标签上有多次ng-click,则只会执行第一次。
<强>样本强>
var myApp = angular.module('myApp', []);
myApp.controller('CookieCtrl', function ($scope) {
$scope.print1 = function () {
alert("first");
}
$scope.print2 = function () {
alert("second");
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div id="button-holder" ng-controller="CookieCtrl">
<button ng-click="print1()" ng-click="print2()">Bump!</button>
</div>
</div>
&#13;
您还可以使用分隔的 ;
在ng-click上执行许多功能。
<强>样本强>
var myApp = angular.module('myApp', []);
myApp.controller('CookieCtrl', function ($scope) {
$scope.print1 = function () {
alert("first");
}
$scope.print2 = function () {
alert("second");
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div id="button-holder" ng-controller="CookieCtrl">
<button ng-click="print1();print2();">Bump!</button>
</div>
</div>
&#13;