Angular $ watch功能无法正常工作

时间:2015-08-31 06:45:11

标签: angularjs watch

所以我在指令的link函数的范围内有以下观察器。我已经将这个模式用于另一个变量,但是它可以正常工作。有没有人有什么建议。代码如下

    $scope.$watch('QOLFactory.surveysInView', function(newValue, oldValue, scope){
        console.log(QOLFactory.surveysInView);
        console.log(newValue, oldValue);
        scope.surveys = newValue;
        scope.surveyData = generateSurveyTotals(newValue);
});

第一个控制台日志打印QOLFactory.surveysInView的corrent值,但第二个console.log打印undefined,undefined。有什么我做错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

为了使watcher on指令能够访问正在控制器中注入的Service,必须将Service分配给$ scope变量。这使得观察者的行为符合预期,并返回正确的值。