3
Inside Factory has a variable countF
value 1:
Factory
app.factory('testFactory', function(){
var countF = 1;
return {
getCount : function () {
return countF;
},
incrementCount:function(){
countF++;
return countF;
}
}
});
Controller
function FactoryCtrl($scope, testService, testFactory)
{
$scope.countFactory = testFactory.getCount;
$scope.clickF = function () {
$scope.countF = testFactory.incrementCount();
};
}
You can change the variable value countF
that is outside the return
at Factory? For example, increase it according to return
of incrementCount
?
Controller? No. Only if you move the
countF
into the object returned by Factory.– bfavaretto
No, I just wish I could change the value of the variable in Factory. Because as far as I know the compiler only passes once in Factory and Functions every time they are called.
– leopiazzoli
But where exactly? I guess I didn’t understand the question then.
– bfavaretto
Sorry for the lack of clarity. I want the
var countF = 1
can switch to the same value as the Controller is sending. Example tovar countF = 2
.– leopiazzoli
Why don’t you create a method
setCount
in Factory?– bfavaretto
How to do this?
– leopiazzoli