AngularJS:控制器更改未反映在html页面中

时间:2015-08-06 14:11:12

标签: javascript angularjs

我们有html代码:



<div ng-controller="LoginController as vm">
    <ul ng-if="vm.user_info">
        <li><a href="/+login">Login</a></li>
    </ul>
</div>
&#13;
&#13;
&#13;

控制器代码为

&#13;
&#13;
(function () {
  'use strict';

  angular
    .module('site.authentication.controllers')
    .controller('LoginController', LoginController);

  LoginController.$inject = ['$location', '$scope', 'Authentication'];

  function LoginController($location, $scope, Authentication) {
    var vm = this;
    vm.login = login;
    vm.user_info = true;
    
    function login() {
      promise = Authentication.login(vm.email, vm.password);
      vm.user_info = false;
    }
})();
&#13;
&#13;
&#13;

[编辑]添加登录名为代码:

&#13;
&#13;
<form role="form" ng-submit="vm.login()">
    <div class="alert alert-danger" ng-show="error" ng-bind="error"></div>
    <input type="text" class="form-control" id="login__email" ng-model="vm.email" placeholder="ex. john@example.com" />        
    <input type="password" class="form-control" id="login__password" ng-model="vm.password" placeholder="ex. thisisnotgoogleplus" />      
    <button type="submit" class="btn btn-primary">Submit</button>
</form>
&#13;
&#13;
&#13;

登录链接显示在登录前的html和登录&#34; vm.user_info&#34;设置为false,但不起作用,登录后仍显示Login链接。

如果上述任何问题,请告诉我们。

1 个答案:

答案 0 :(得分:0)

这是备用解决方案。

在vm.user_info上使用$ timeout。

(function () {
  'use strict';

  angular
    .module('site.authentication.controllers')
    .controller('LoginController', LoginController);

  LoginController.$inject = ['$location', '$scope', 'Authentication'];

  function LoginController($location, $timeout, $scope, Authentication) {
    var vm = this;
    vm.login = login;
    vm.user_info = true;

    function login() {
      promise = Authentication.login(vm.email, vm.password);
      $timeout(function(){
          vm.user_info = false;
      }, 100)
    }
})();