Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply changes in angularjs (ng-show)?

Tags:

angularjs

I have form

<form ng-controller="SessionController" name="login_form">
  <div class="hide" ng-show='invalid_credentials'>Invalid email/pass</div>
  <input type="submit" value="Login" ng-click='login()'>
</form>

Controller:

utApp.controller('SessionController', ($scope, $cookieStore, ClientService) ->
  $scope.login = ->
    ...
    if something
      $scope.invalid_credentials = true
      return

on some conditions $scope.invalid_credentials is getting set to true, but div with error message is not being shown. How to show it?

like image 233
sl_bug Avatar asked Oct 09 '12 12:10

sl_bug


People also ask

How do you use NG on change?

Definition and UsageThe ng-change event is triggered at every change in the value. It will not wait until all changes are made, or when the input field loses focus. The ng-change event is only triggered if there is a actual change in the input value, and not if the change was made from a JavaScript.

What is Ng-show in AngularJS?

The ng-show Directive in AngluarJS is used to show or hide the specified HTML element. If the given expression in the ng-show attribute is true then the HTML element will display otherwise it hides the HTML element. It is supported by all HTML elements.

What is Ng-change in AngularJS?

The ng-change Directive in AngularJS is used whenever the value of an input element changes. The expression is evaluated immediately whenever there is a change in the input value. It requires an ng-model directive to be present. It is triggered whenever there is any single change in the input.

Can we use ng-show and Ng-hide together?

Absolutely not. First of all, the two directives can trip over each other( see this JSFiddle, as provided by Joel Skrepnek), and is generally just bad design.


2 Answers

Angular doesn't re-check the value of your invalid_credentials variable. Using a function as your ng-show argument should work.

like image 118
Simon Ernst Avatar answered Sep 20 '22 12:09

Simon Ernst


The accepted answer will work... However, what you really needed to do is use $scope.$apply

utApp.controller "SessionController", ($scope, $cookieStore, ClientService) ->
  $scope.login = ->
    if something
      $scope.$apply (s) ->
        s.invalid_credentials = true

Generally, when you update the scope, and it doesn't update the UI... it's because you need to use $apply.

like image 34
Ben Lesh Avatar answered Sep 18 '22 12:09

Ben Lesh