AngularJS: Can't I set a variable value on ng-click?

Angularjs

Angularjs Problem Overview


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

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

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

Angularjs Solutions


Solution 1 - Angularjs

Just do:

ng-click="prefs = false"

Solution 2 - Angularjs

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

ng-click="hidePrefs()"

and then in your controller:

$scope.hidePrefs = function() {  
  $scope.prefs = false;
}

Solution 3 - Angularjs

You can use some thing 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>

Solution 4 - Angularjs

If you are using latest versions of Angular (2/5/6) :

In your component.ts

//x.component.ts
prefs = false;

hidePrefs(){
   this.prefs = true;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjgravoisView Question on Stackoverflow
Solution 1 - AngularjstymeJVView Answer on Stackoverflow
Solution 2 - AngularjsmsarchetView Answer on Stackoverflow
Solution 3 - AngularjsBhawna MalhotraView Answer on Stackoverflow
Solution 4 - Angularjsuser4148098View Answer on Stackoverflow