如何在指令中使用$ scope.watch作为范围?

时间:2014-07-02 13:30:21

标签: angularjs angularjs-directive angularjs-scope

我想在我的自定义指令中查看模型search.value

怎么做?

HTML:

<div search></div>

指令:

app.directive('search', function($rootScope) {
  return {
    restrict: 'A',
    transclude: true,
    scope: true,
    templateUrl: 'template/app/inputSearch.html',
    replace: true,
    controller: function() {

    },
    link: function (scope, element) {

        /* ... */
        scope.$watch(......., function(value) {

        });

    }
  };
});

模板:

<div class="search-parent">
  <div class="search">
    <input type="text" ng-model="search.value" />
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

controller: function($scope) { 
    $scope.$watch('search.value', function(newVal, oldVal) {

    });
}