Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs ng-if difference between value and function

Is there any difference using ng-if with a value or with a function ?

ng-if="myvalue"
ng-if="myfunc()"

UPDATE (for a better understanding why I'm asking for)

html

<div class="navbar navbar-default navbar-static-top" data-ng-controller="NavController as nav">
            <div class="container">
                <ul class="nav navbar-nav">
                    <a data-ui-sref="home" class="navbar-brand"><i class="logo"></i> Angular Express</a>
                    <li ui-sref-active="active"><a data-ui-sref="home">Home</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right" data-ng-if="!nav.isAuthenticated()">
                    <li><a data-ui-sref="session.login">Log in</a></li>
                    <li><a data-ui-sref="session.signup">Sign up</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right" data-ng-if="nav.isAuthenticated()">
                    <li><i class="fa fa-user"></i> <span ng-bind="nav.isAuthenticated().username"></span> <a href="/api/auth/logout" data-ng-click="nav.logout()">Logout</a></li>
                </ul>
            </div>
        </div>

js

function NavController($rootScope, UserStorage){
    var nav = this;
    nav.isAuthenticated = function() {
        UserStorage.get();
    }; 
}
function UserLoginController($rootScope,$state, Users, UserStorage) {
    var user = this;
    user.data = {};
        user.save = function() {
            Users.login(user.data).then(function(response) {
            console.log(response.data);
            UserStorage.set(response.data);
            $state.go('home');
        })
        .catch(function(response) {
            console.log(response);
            user.errors = response.data;
        });
    };
}

If I use like this I've got a $digest() iterations reached

RE UPDATE

(for chandermani comment)

function UserStorage($sessionStorage) {
  return {
    set: function(data) {
        $sessionStorage.user = angular.toJson(data);
    },
    get: function() {
        return angular.fromJson($sessionStorage.user); 
    },
    del: function() {
        delete $sessionStorage.user;
    }
  };
}
like image 849
Whisher Avatar asked Jan 09 '15 09:01

Whisher


People also ask

What is difference between Ng-if and Ng-show?

ng-if can only render data whenever the condition is true. It doesn't have any rendered data until the condition is true. ng-show can show and hide the rendered data, that is, it always kept the rendered data and show or hide on the basis of that directives.

What is NG-if in AngularJS?

AngularJS ng-if Directive The ng-if directive removes the HTML element if the expression evaluates to false. If the if statement evaluates to true, a copy of the Element is added in the DOM.

What is the difference between ngIf and * ngIf?

What is the difference between ngIf and *ngIf in Angular? ngIf is the directive. Because it's a structural directive (template-based), you need to use the * prefix to use it into templates. *ngIf corresponds to the shortcut for the following syntax (“syntactic sugar”):

Can you use Ng-if and Ng-show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.


1 Answers

For angular both are expression, that it evaluates in context of current scope. Angular does this on each digest cycle.

There are more ways to shoot in the foot if you are using the function way. myfunc could do

$scope.myfunc=function() {
   //do some time consuming work
   return data;
};

In such a case the binding evaluation on each digest cycle will make your binding and app slow.

So if you are using function based binding make sure that functions return fast by doing minimum processing.

like image 187
Chandermani Avatar answered Oct 17 '22 08:10

Chandermani