Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在指令中触发范围监视?_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

Angularjs 如何在指令中触发范围监视?

Angularjs 如何在指令中触发范围监视?,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我正试图使用scopewatcher触发我的指令代码: var gMaps = function($timeout, $q, GeoCoder, mockdataService) { return { restrict: 'EA', template: '<div class="gmaps"></div>', replace: true, link: function postLink(scope

我正试图使用scopewatcher触发我的指令代码:

var gMaps = function($timeout, $q, GeoCoder, mockdataService) {
      return {
        restrict: 'EA',
        template: '<div class="gmaps"></div>',
        replace: true,
        link: function postLink(scope, iElement, iAttrs) {

          $scope.watch(scope.lat, function() {...
当我更改表单上的lat属性时,它不会触发指令上的函数

你只是把美元放错地方了$watch是附加到您命名为scope的变量的方法。只需使用scope.$watch而不是$scope.watch。

首先,您必须使用scope.$watch。。。而不是$scope。看。。。因为在链接函数中有scope而不是$scope作为参数。你还错过了$BEFOR watch

你也应该像这样使用$watch

scope.$watch('lat',function(newVal, oldVal){
   //this will watch scope.lat
});

scope.$watch(function(){
   return scope.lat
},
function(newVal, oldVal){
  //this function will be fired everytime first function return other value than before
});