AngularJS watch an object property in an object array

后端 未结 4 1619
日久生厌
日久生厌 2021-02-06 20:56

I have this data in my controller

    $scope.data = {
        home: {
            baseValue: \"1\",
            name: \"home\"
        },
        co         


        
4条回答
  •  不要未来只要你来
    2021-02-06 21:26

    In this kind of scenario there is no way to circumvent utilizing multiple watches, another way to do this is by utilizing $watchCollection to watch the array of object values, you can get this array using the Object.values function.

    scope.$watchCollection(function() {
        return Object.values(obj);
    }, function(newValues, oldValues) {
        // now you are watching all the values for changes!
        // if you want to fire a callback with the object as an argument:
        if (angular.isFunction(scope.callback())) {
            scope.callback()(obj);
        }
    });
    

提交回复
热议问题