AngularJS : 更改未在 `$watch` 方法中触发的对象

AngularJS : changing the object not triggered in `$watch` method

作为基础,我保留了一个具有一些默认值的对象。一旦我从服务器获取值,我就会更新对象。当我更新时,不会触发 $watch 方法。有谁能帮我知道原因吗?

我的 controller.js :

$scope.graphInfo = {"ActualPercentage" : 30, "Type" : "Mechanical" };
        // default sets

        $scope.conractorInfo = function ( contractor ) {

            $location.search('id', contractor.Id);

            server.contractor.get({id:$routeParams.id, contid:contractor.Id}).$promise.then(function (data) {

                $scope.contractor = data;
                $scope.graphInfo.ActualPercentage = Number($scope.contractor.ActualPercentage);
                $scope.graphInfo.Type = $scope.contractor.Type;
                //updating object
            });

        }

我的模板带有指令:

    <plan-vs-actual data="graphInfo"></plan-vs-actual>

//getting graph info.

我的指令:

    var planVsActual = function ($timeout) {

        return {

            replace : true,

            scope : {

                data : '='
            },

            template : "<div id='pieGraph'></div>",

            link : function ( scope, element, attr ) {


                var width = element.width(), 
                height = element.height(),
                radius = Math.min(width, height) / 1.2;

                var color = d3.scale.ordinal().range(["#ffff00", "#1ebfc5"]);

                var pie = d3.layout.pie().sort(null).value(function(d) { return d });

                var arc = d3.svg.arc().outerRadius(radius - 90).innerRadius(radius - 85);

                var svg = d3.select("#pieGraph").append("svg").attr("width", width).attr("height", height)
                         .append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

                scope.$watch ('data', function ( newValue, oldvalue ) {

                    console.log( newValue );
//after i am updating using server values, not updating here.

                });
    }
    }
    }

$watch 的第 3 个参数设置为 true

修改喜欢,

scope.$watch ('data', function ( newValue, oldvalue ) {

    console.log( newValue );

 }, true);

// put true to watching properties

第三个参数是 objectEquality 它将使用 angular.equals 比较对象相等性而不是比较引用相等性。

这里是 DOC

不确定,试一试。