AngularJS: Can't set the value of a variable to ng-click?

I have a modal that uses ng-show = "prefs" to determine visibility.

My desire is to use the close button in modal to set the $ scope.prefs value to false and use the anchor tag to set the value to true. However, everything I can find on google uses a checkbox, not anchor tags.

Is there a way to use ng-click to set a variable to false?

+44
angularjs
Apr 17 '14 at 18:17
source share
3 answers

Just do:

ng-click="prefs = false" 
+73
Apr 17 '14 at 18:21
source share

While @tymeJV gave the correct answer, a way to do this to be inline with angular would be this:

ng-click="hidePrefs()"

and then in your controller:

 $scope.hidePrefs = function() { $scope.prefs = false; } 
+16
Apr 17 '14 at 18:53
source share

You can use something like this

 <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> </head> <body> <div ng-app="" ng-init="btn1=false" ng-init="btn2=false"> <p> <input type="submit" ng-disabled="btn1||btn2" ng-click="btn1=true" ng-model="btn1" /> </p> <p> <button ng-disabled="btn1||btn2" ng-model="btn2" ng-click="btn2=true">Click Me!</button> </p> </div> </body> </html> 
+1
Jan 24 '15 at 7:37
source share



All Articles