Corner JS scope. $ see whole object

Can I watch the entire object in angular format?

I created a plunker to show you what I mean: http://plnkr.co/edit/QxLzbwKkJ5k50DiP8OP1

When I change the value of RG or B, the colors object does not fire the change event, only the value of RG or B fires. Can I fire the event myself so that it knows that the whole object has changed?

In addition to the plunker, there is a code:

function ColorObject()
{
    this.r = 0;
    this.g = 0;
    this.b = 0;

    this.c = 0;
    this.m = 0;
    this.y = 0;
    this.k = 0;

    this.hex = "#ff0000";
    this.oppositeHex = "#00ffff";
}

var chColorChanger = angular.module('chColorChanger',[]);

chColorChanger.controller('chColorChangerCtrl', function ($scope,$document,$element) 
{     
    $scope.colors = new ColorObject();
    $scope.gMessage = 0;
    $scope.message = 'scope initialized';
});

chColorChanger.directive('chColorValue',['$document',function($document) {
    return {
        link: function (scope, elem, attrs) {

            scope.$watch('colors',function(newValue,oldValue){
               scope.gMessage++;
            });

            scope.$watch('colors.r', function (newValue,oldValue) {
                scope.message = 'r changed';
            });

            scope.$watch('colors.g', function (newValue,oldValue) {
                scope.message = 'g changed';
            });

            scope.$watch('colors.b', function (newValue,oldValue) {
                scope.message = 'b changed';
            });
        }
    }
}]);
+4
source share
1 answer

To view the entire object, you need to use the deep clock. which needs a third parameter to view as true ...

        scope.$watch('colors',function(newValue,oldValue){
           scope.gMessage++;
        }, true);

plunker

+4

All Articles