$rootScope、$apply、$watch

$rootScope:$rootScope是所有$scope对象的最上层,对全部controller有效

app.run(function ($rootScope) {
        $rootScope.sex = "";
    });

也可以用在控制器内;

$apply:AngularJs外部的控制器(DOM事件、外部的回调函数如JQuery UI等第三方控件),调用AngularJs函数后,就必须使用$apply()。

setTimeout(function () {
            $scope.$apply(function () {
                $scope.name = "嘿嘿";
            })
        }, 1000)

$watch:监听model变化,尽量不要再controller内使用

    app.directive("err", function () {
        return {
            restrict: "E",  
            template: "<input ng-model='txt'>",
            link: function (scope, element, attrs) {
                scope.$watch("txt", function (a) {
                    if (a == "err") {
                        alert(1);
                    }
                })
            }
        }
    })

 

posted on 2018-03-27 10:38  段了的弦  阅读(198)  评论(0编辑  收藏  举报