角$手表|返回从功能项目 [英] Angular $watch | returning the item from function

查看:95
本文介绍了角$手表|返回从功能项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣找出为什么我总是要做到这一点

  $范围。$表(函数(){
   返回$ scope.someData;
},功能(价值){
   的console.log(值);
});

角形实际观看的数据,为什么我必须这样做,这是真正的错误我,因为它看起来毫无意义的事情之一。

如果我做这样的事情。

  $范围。$腕表($ scope.someData,功能(价值){
   的console.log(值);
});

这是更好的,它永远不会奏效?

我也用这个了很多工厂

说, $数据是一个工厂我必须做的。

  $范围。$表(函数(){
   返回$ data.someData;
},功能(价值){
   的console.log(值);
});


解决方案

这个作品:

  $范围。$表(someData功能(价值){
   的console.log(值);
});

I'm interested to find out why i always have to do this

$scope.$watch( function() {
   return $scope.someData;
}, function( value ) {
   console.log( value );
});

for angular to actually watch the data, why do I have to do this, this is one of the things that really bug me because it looks pointless.

If I do something like this

$scope.$watch($scope.someData, function( value ) {
   console.log( value );
});

Which is nicer, it never works?

I also use this a lot with factories

say that $data is a factory I have to do

$scope.$watch( function() {
   return $data.someData;
}, function( value ) {
   console.log( value );
});

解决方案

This works:

$scope.$watch("someData", function( value ) {
   console.log( value );
});

这篇关于角$手表|返回从功能项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆