没有错误是可见的。这个 ng-switch 指令有问题还是我在其他地方有问题?
Controller
app.controller("MyCtrl", ["$scope", function($scope) {
$scope.model.error = 1;
}]);
查看
<div ng-switch="model.error">
<label ng-switch-when="1">Error: Username do not exists.</label>
<label ng-switch-when="2">Error: Password is incorrect.</label>
<label ng-switch-when="3">Error: Username is already taken.</label>
</div>
最佳答案
ngSwitch 没有任何问题。问题是您没有初始化变量 $scope.model 因此您一定在浏览器控制台中遇到错误。
TypeError: Cannot set property 'error' of undefined
使用
app.controller('MainCtrl', function($scope) {
$scope.model = {}; //Added the line to initializing variable.
$scope.model.error = 1;
});
关于javascript - AngularJS 简单的 ng-switch 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278235/