55

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?

2
  • 1
    both inside the quotes ... that was what I was missing. Thanks! Commented Apr 17, 2014 at 18:20
  • 1
    I know it's exactly a year later, but you commented on your own answer, not @tymeJV's.... I thought it was funny. Commented Apr 17, 2015 at 21:42

4 Answers 4

96

Just do:

ng-click="prefs = false"
Sign up to request clarification or add additional context in comments.

8 Comments

@AshokGujjar -- ng-click="prefs = false; refr = true;"
this doesn't work for me, only doing it in the contorller (see msarchets answer) does it. any idea why this could happen?
@Blauhirn Had the same problem. I dont know why, but when I use an object it works. ng-click="foobar.prefs = false"
@ChristianNilsson -- Probably a scope inheritance issue, read: github.com/angular/angular.js/wiki/Understanding-Scopes - it's definitely worth it.
@Ankita you can set it like ng-click="refr = !prefs" or ng-click="refr = prefs == true ? false : true"
|
22

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

Comments

2

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>

Comments

1

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

In your component.ts

//x.component.ts
prefs = false;

hidePrefs(){
   this.prefs = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.