Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  131] [ 6]  / answers: 1 / hits: 25641  / 12 Years ago, thu, may 24, 2012, 12:00:00

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.


More From » angularjs

 Answers
8

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/


[#85377] Wednesday, May 23, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deiong

Total Points: 15
Total Questions: 103
Total Answers: 99

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
deiong questions
Mon, Nov 22, 21, 00:00, 3 Years ago
Tue, Jun 15, 21, 00:00, 3 Years ago
Mon, Dec 21, 20, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
;