Service variable not updating in controller

前端 未结 3 1769
长情又很酷
长情又很酷 2020-12-07 16:13

I have a NavbarCtrl that is outside of ng-view. I have a login controller that talks to a service to get a user logged in. Once the user is logged in, I want the Navbar to u

3条回答
  •  庸人自扰
    2020-12-07 16:41

    Try to change

    $scope.$watch(Auth.isAuthenticated(),function () { $scope.user = Auth.getUser() })

    to

    $scope.$watch(Auth.isAuthenticated(),function () { $scope.user = Auth.getUser() }, true)

    Look at the AungularJs $watch documentation for a detailed explanation, the default value is false which means that Angular detects changes in the first parameter values by reference, while passing true means the check is done by equality. Being the parameter a function, the check returns always false, since the reference to the function is always the same.

提交回复
热议问题