Angularjs 如何在指令中触发范围监视?
我正试图使用scopewatcher触发我的指令代码: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
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
});