Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.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
Javascript &引用;scope.watch不是一个函数;Angular.js中的错误_Javascript_Angularjs_Angularjs Directive_Angularjs Scope_Watch - Fatal编程技术网

Javascript &引用;scope.watch不是一个函数;Angular.js中的错误

Javascript &引用;scope.watch不是一个函数;Angular.js中的错误,javascript,angularjs,angularjs-directive,angularjs-scope,watch,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Watch,我已经编写了一个自定义指令来格式化值,下面给出了自定义指令 var directiveapps = angular.module('myApp.currencyValues', []); directiveapps.directive('currencyValue', function () { return{ scope: { data: '=items' }, template: '<div>$ {{value|number}}</div>', link: f

我已经编写了一个自定义指令来格式化值,下面给出了自定义指令

var directiveapps = angular.module('myApp.currencyValues', []);
directiveapps.directive('currencyValue', function () {
return{
scope: {
    data: '=items'
},
template: '<div>$ {{value|number}}</div>',
link: function(scope){
  scope.value = Math.round(scope.data* 100) / 100;
}
};
}); 
var directiveapps=angular.module('myApp.currencyValues',[]);
directiveapps.directive('currencyValue',函数(){
返回{
范围:{
数据:'=项'
},
模板:“${value | number}}”,
链接:功能(范围){
scope.value=Math.round(scope.data*100)/100;
}
};
}); 
此指令将值格式化为货币,并将小数点舍入。我从这样的角度调用了这个指令

<div class="overallsummary_meter_txt left">
Total Price<br>
<span currency-value items="totalPrice" class="orange_txt"></span>
</div>

总价
这个很好用。但是,当通过ajax将值传递给视图时,我发现了一些问题。该指令在加载视图时执行,如果加载值需要时间,那么变量items中将不会有任何值。因此,我从指令中得到一个空值。 为了解决这个问题,我稍微修改了我的指令,如下所示

var directiveApps = angular.module('gogocarApp.currencyValues', []);
directiveApps.directive('currencyValue', function () {
return {
transclude: true,
scope: {
  items: '=items'
},
template: '<div>$ {{items|number}}<span ng-transclude></span></div>',
link: function(scope){
  scope.watch('items', function () {
   scope.items = scope.items ? Math.round(scope.items* 100) / 100 : 0;
  });
}
};
}); 
var directiveApps=angular.module('gogocarApp.currencyValues',[]);
directiveApps.directive('currencyValue',函数(){
返回{
是的,
范围:{
项目:'=项目'
},
模板:“${{items | number}}”,
链接:功能(范围){
scope.watch('items',function(){
scope.items=scope.items?数学四舍五入(scope.items*100)/100:0;
});
}
};
}); 
我添加了一个scope.watch函数来解决我现有的问题。这对所有的瞬间都很有效,我得到了格式化和正确的值作为指令的输出

但作为一种后遗症,在使用此指令的每个页面中,都会显示控制台错误scope.watch不是一个函数

1.如何消除此控制台错误


2.是否需要再次修改指令?

watch方法的名称为
$watch
。参考文件:

因此,你应该:

scope.$watch('items', function () {
   scope.items = scope.items ? Math.round(scope.items* 100) / 100 : 0;
});

它应该是
$watch
,而不是
watch
。它是
范围。$watch
而不是
$scope。watch
我对代码进行了更改,控制台错误不再存在。但是有些页面我得到“0”作为指令的输出,这可能是由于在获得变量项的值之前执行了指令,但是我已经包括了这个范围。$watch克服了这个问题。。有没有其他办法解决这些问题