javascript - 如果没有 $scope.digest,$watch 不会触发

标签 javascript angularjs watch

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

<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/

相关文章:

angularjs - Angular js工厂调用 "is not a function"

linux - 查看目录中的新文件,然后运行脚本

node.js - 使用 Node uglify + watch 合并目录中的 javascript 文件?

javascript - 使用 :nth-child() selector and . get() 方法之间的性能差异?

mysql - 自动刷新$scope

javascript - 使用范围变量定义时,ng-class 在 AngularJS 中不起作用

macos - 监视特定文件更改然后捕获它们的 Shell 脚本

javascript - 单击按钮后,计数 x 秒,然后使用 jquery 重定向到特定链接

javascript - JS重命名方法

javascript - 根据内容自动调整文本区域的大小