为什么$ watch不能在我的指令中工作?

时间:2015-04-12 14:56:30

标签: angularjs angularjs-directive angularjs-watch

我的指令定义为:

angular.module('mainCtrl').directive('countryCtrl', ['$compile', 'Country', function($compile, Country) {

   return {
        require: "^widgetPath",
        restrict: 'E',
        scope:{
               selectedCountry: '='
        },
        link: function(scope, element, attrs, widgetPath) {

    scope.$watch("selectedCountry", function () {
                    alert ("this never get called");
                },true);
                console.log(widgetPath.model);
            }
        };
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<country-ctrl id="countryCtrl" class="form-inline">
    <ui-select ng-model="selectedCountry" theme="selectize" style="{{$cat_style}}">
        <ui-select-match placeholder="Select or search ...">
    @{{$select.selected.title}}
    </ui-select-match>
    <ui-select-choices repeat="country in countries | filter: $select.search">
        <span ng-bind-html="country.title | highlight: $select.search"></span>
    </ui-select-choices>
    </ui-select>
    <p class="text-center" ng-show="loading"><span class="throbber"></span></p>

</country-ctrl>

当我更改所选国家/地区时,$watch无效,为什么?范围正常,因为ng-show工作正常。

我确信我缺少一些简单的事情,有人可以解释一下吗?

0 个答案:

没有答案