我正在编写自己的指令,它是一个字典:
<div ng-controller="formController">
<dictionary
label="Editable"
model="bso.dictionnaryObject"
required=true
dictionary-object="dico">
</dictionary>
</div>
“dico”值是通过 Controller 中的 GET 调用设置的,因此这意味着它不会在指令初始化时初始化
所以我对我的指令戴上了 watch
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 是我发现触发 watch 的唯一方法。 有什么方法可以不调用 $scope.digest() 并触发我的 watch 吗?
谢谢
最佳答案
这不起作用,因为 $.get()
不知道应该执行摘要循环。
您应该使用 Angular 方式:
$http.get("http://framework:8787/Services/ofo/rest/dictionary/OrganisationStatusAvailabilityCode",
function (data) {
$scope.dico = data;
//$scope.$digest(); If you use $http, the digest it's implicit
});}
关于javascript - 如果没有 $scope.digest,$watch 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36548848/