ng-include causes the controller block to re render

后端 未结 3 600
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 08:02

I am trying to use ng-switch with ng-include below. The problem is with ng-init and the whole controller block getting re-rendered on any ng-includes change.

In the

3条回答
  •  臣服心动
    2020-12-13 08:32

    I've have a working example. The trick is that the scope variable to be evaluated has to be an object, not a primitive type. It looks like $scope.$watch() is not watching primitive types properly (which is a bug). The jsFiddle has a parent controller with two child controllers, but it would also work with only one (parent) controller.

    HTML:

    Controller:

    function LoginCheckCtrl($scope) {
        $scope.user = {
            login: false
        };
    }
    

    Note: this will also work with only one controller:

    function LoginCheckCtrl($scope) {
        $scope.user = {
            login: false
        };
        $scope.login = function() {
            console.log($scope.user.login ? 'logged in' : 'not logged in');
            $scope.user.login = true;
        };
        $scope.logout = function() {
            console.log($scope.user.login ? 'logged in' : 'not logged in');
            $scope.user.login = false;
        };
    }
    

提交回复
热议问题