最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-style not changing with property - Stack Overflow

programmeradmin1浏览0评论

I cant seem to figure out why the style property is not getting updated. In my larger application it seems to work fine.

angular.module('Model', [])
    .factory('SizeModel', function () {
        return {
            width: 200,
            height: 100,
            display:"block",
            getCombined: function() {
                return parseInt(this.width) + parseInt(this.height);
            }
        };
    });

function AlbumCtrl($scope,SizeModel) {
    $scope.master = SizeModel;
    $scope.$watch("master",function(){
    $scope.myprop = { display: $scope.master.display,
                      backgroundColor: "#333",
                      width: $scope.master.width+'px',
                      height: $scope.master.height+'px',
                      color: "#FFF"
                     };
        });

}

function AnoCtrl($scope,SizeModel) {
    $scope.master = SizeModel;

    $scope.toggle = function(){
        $scope.master.display = "none";
    }
}

function EditCtrl($scope,SizeModel) {
    $scope.master = SizeModel;
}

/

Here is a fiddle which shows the issue I am currently facing. You will notice that the width and height are getting updated in the div when you change the input. But the style itself doesnt seem to be updating. Anyone can tell me what I am doing wrong here?

I have tried all the following scenarios

  1. using $scope.$apply.. - Throws an error stating $apply already in progress..
  2. $rootScope.$apply - same as above.
  3. Setting another variable in a service which is $watched in the other controller. - no change seen.

It would be really cool if someone can get me an answer to this. Also would be really happy if you can tell me why exactly it is not getting updated.

I cant seem to figure out why the style property is not getting updated. In my larger application it seems to work fine.

angular.module('Model', [])
    .factory('SizeModel', function () {
        return {
            width: 200,
            height: 100,
            display:"block",
            getCombined: function() {
                return parseInt(this.width) + parseInt(this.height);
            }
        };
    });

function AlbumCtrl($scope,SizeModel) {
    $scope.master = SizeModel;
    $scope.$watch("master",function(){
    $scope.myprop = { display: $scope.master.display,
                      backgroundColor: "#333",
                      width: $scope.master.width+'px',
                      height: $scope.master.height+'px',
                      color: "#FFF"
                     };
        });

}

function AnoCtrl($scope,SizeModel) {
    $scope.master = SizeModel;

    $scope.toggle = function(){
        $scope.master.display = "none";
    }
}

function EditCtrl($scope,SizeModel) {
    $scope.master = SizeModel;
}

http://jsfiddle.net/ganarajpr/C2hRa/4/

Here is a fiddle which shows the issue I am currently facing. You will notice that the width and height are getting updated in the div when you change the input. But the style itself doesnt seem to be updating. Anyone can tell me what I am doing wrong here?

I have tried all the following scenarios

  1. using $scope.$apply.. - Throws an error stating $apply already in progress..
  2. $rootScope.$apply - same as above.
  3. Setting another variable in a service which is $watched in the other controller. - no change seen.

It would be really cool if someone can get me an answer to this. Also would be really happy if you can tell me why exactly it is not getting updated.

Share Improve this question edited Aug 18, 2017 at 4:41 PEHLAJ 10.1k10 gold badges42 silver badges56 bronze badges asked May 24, 2012 at 11:52 GanarajGanaraj 26.8k6 gold badges67 silver badges60 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

You had assigned the width and height values to the myprop style field on a one-off basis. So when you changed the width or height the myprop was not changing.

Change the myprop value to a function that computes its value instead...

http://jsfiddle.net/DyHHJ/

Your $scope.$watch in the AlbumCtrl only watches reference inequality, which is determined by strict comparison via the !== Javascript operator. Because you are changing only a property on the object, the reference to master is still the same and doesn't fire the $watch. You need to pass an additional argument to $watch to use objectEquality. See this plunkr, and here is the relevant code below:

$scope.$watch("master",function() {
  $scope.myprop = {
    display: $scope.master.display,
    backgroundColor: "#333",
    width: $scope.master.width+'px',
    height: $scope.master.height+'px',
    color: "#FFF"
  };
}, true);

The third argument true tells angular to determine inequality of the watchExpression by the angular.equals function. It correctly determines that the the updated master object has been changed, and updates myprop

发布评论

评论列表(0)

  1. 暂无评论