i am Trying to update a Solution from 1.2.1 to 1.3.15 but i got stuck it seems that there have been a change how the digest Cycle run through loops.
I have a Factory which holds data For all My controller it has a list of Objects and a Current Object Property. It is used for Currency Conversion.
Then i got a filter which can be put on Number values. It references the Factory and uses the Current Property to get the Conversion Rate and modifies the output.
Value = 1000; Current= kiloCurrency= 1000 => {Value | bvcurrency} Outputs: 1;
There is a Function to change the current currency and the Filter should update the output.This worked fine in 1.2.1 but not in 1.3.x
I got some Examples:
Not working 1.3.15: http://jsfiddle.net/dabii/mqkj4td9/7/
Working: 1.2.1: http://jsfiddle.net/dabii/mqkj4td9/8/
app.factory("numberSvc", [function () {
return {
Formats: [{
Display: "LC",
Multiply: 1
}, {
Display: "kLC",
Multiply: 1000
}, {
Display: "MLC",
Multiply: 1000000
}],
Current: {
Display: "kLC",
Multiply: 1000
},
AddCurrency:function(curr){
this.Formats.push(curr);
},
SetCurrent: function(format){
this.Current = format;
}
};
}]);
app.filter('bvcurrency', ['$filter', 'numberSvc', function ($filter, numberSvc) {
return function (input) {
var multi = numberSvc.Current.Multiply,
ret = ( !! input || input === 0) && angular.isNumber(parseFloat(input)) ? $filter('number')(input / multi, 0) : "";
return ret;
};
}]);
By pressing the buttons you can see that the Factory Value changes but not the one using the filter neither aut nor inside the repeat. If i force a Digest by changing a Value then the Value outside repeat will change but not the inside.
It seems that there is some kind of "intelligent" digest running and seeing that the values inside the repeat did not changed and so will not run the digest for them. But it does not see that the output will change because it does not take the filter in account.
Can somebody point me in the right direction to get it working in 1.3.1? Or showing me the mistake?
Kind Regards
Rafael
By default filters are considered stateless, that is the output value only depends on the input value. This gives Angular the chance to optimize: As long as the input value doesn't change, the output value doesn't have to be recalculated. This behavior has changed with 1.3.
Your filter is stateful, so you have to add the $stateful
flag in order to let Angular know.
app.filter('bvcurrency', ['$filter', 'numberSvc', function ($filter, numberSvc) {
function filter(input) {
var multi = numberSvc.Current.Multiply,
ret = ( !! input || input === 0) && angular.isNumber(parseFloat(input)) ? $filter('number')(input / multi, 0) : "";
return ret;
};
filter.$stateful = true;
return filter;
}]);