MR.ABC
MR.ABC

Reputation: 4862

AngularJS Simple ng-switch not working

No error is visible. Is something wrong with this ng-switch directive or do i've an issue somewhere else ?

Controller

app.controller("MyCtrl", ["$scope", function($scope) {
    $scope.model.error = 1;
}]);

View

<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>

Upvotes: 1

Views: 103

Answers (1)

Satpal
Satpal

Reputation: 133403

There is nothing wrong with ngSwitch. Problem was that you were not initializing your variable $scope.model hence you must be getting error in your browser console.

TypeError: Cannot set property 'error' of undefined

Use

app.controller('MainCtrl', function($scope) {
   $scope.model = {}; //Added the line to initializing variable.
   $scope.model.error = 1;
});

DEMO

Upvotes: 4

Related Questions