没有 $scope.digest 就不会触发 $watch

$watch not trigger without $scope.digest

我正在编写自己的指令,它是一个字典:

<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() 并触发我的手表?

谢谢

这不起作用,因为 $.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
        });}