Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angularjs

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?

like image 586
jgravois Avatar asked Apr 17 '14 18:04

jgravois


People also ask

Can we put condition in NG-click?

We can add ng-click event conditionally without using disabled class.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

What is the difference between Ng-click and Onclick?

Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.


3 Answers

Just do:

ng-click="prefs = false"
like image 98
tymeJV Avatar answered Oct 22 '22 12:10

tymeJV


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;
}
like image 22
msarchet Avatar answered Oct 22 '22 11:10

msarchet


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>
like image 2
Bhawna Malhotra Avatar answered Oct 22 '22 11:10

Bhawna Malhotra