【发布时间】:2016-08-01 14:58:53
【问题描述】:
我正在编写自己的字典指令:
<div ng-controller="formController">
<dictionary
label="Editable"
model="bso.dictionnaryObject"
required=true
dictionary-object="dico">
</dictionary>
</div>
“dico”值是通过控制器中的 GET 调用设置的,因此这意味着它没有像指令初始化时那样初始化
所以我留意了我的指令
angular.module('dictionary', []).directive('myCustomer', function() {
return {
templateUrl: "dictionary.html",
restrict: = "E",
require: "^form",
scope : {
model: "=",
dictionaryObject: "=",
label: "@",
editable: "@",
},
link : function() {
$scope.$watch("dictionaryObject", function(newVal, oldVal){
console.log(oldVal);
console.log(newVal);
}, true);
}};});
从 rootScope :
app.controller('formController', function ($scope) {
$.get("http://framework:8787/Services/dictionary",
function (data) {
$scope.dico = data;
$scope.$digest();
});}
所以添加 $scope.$digest 是我发现触发手表的唯一方法。 有什么办法不调用 $scope.digest() 并触发我的手表?
谢谢
【问题讨论】:
-
你应该手动
$apply你的$scope更改,而你不使用像$http这样的角度服务,已经可以做到这一点。 -
如果您使用 jquery 或任何其他 javascript,您需要手动消化 \ 应用更改,因为 angular 会丢失 angular 循环之外的代码轨迹。
标签: javascript angularjs watch