使用是否有任何区别
ng-if是一个值还是一个函数?
原文链接:https://www.f2er.com/angularjs/143266.htmlng-if是一个值还是一个函数?
ng-if="myvalue" ng-if="myfunc()"
更新(为了更好地理解我要求的原因)
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; }); }; }
如果我使用这样的话,我就得到了$digest()迭代
重新更新
(对于chandermani评论)
function UserStorage($sessionStorage) { return { set: function(data) { $sessionStorage.user = angular.toJson(data); },get: function() { return angular.fromJson($sessionStorage.user); },del: function() { delete $sessionStorage.user; } }; }