AngularJS ng-style not changing with property

后端 未结 2 1883
感情败类
感情败类 2021-02-04 14:14

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\', [])
    .fact         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 14:19

    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

提交回复
热议问题