I have two controllers, and in one of them I declared the variable $ scope, which I would like to see in the second controller.
First controller
app.controller('Ctrl1', function ($scope) {
$scope.variable1 = "One";
});
Second controller
app.controller('Ctrl2', function ($scope, share) {
console.log("shared variable " + share.getVariable());
});
I explored the best Angular approach, and I found that this is using the service. So I added a service forCtrl1
Service
.service('share', function ($scope) {
return {
getVariable: function () {
return $scope.variable1;
}
};
});
This code returns this error:
Unknown provider: $scopeProvider <- $scope <- share
So my question is: is $ share variable possible between controllers? Not the best Angular solution or am I missing something?
Sorry for my trivial question, but I'm starting Angular.
Thanks in advance
Hi
source
share