I am new to angularjs. I am trying to find out when to use value vs factory as a service. Here is my simple code from egghead.io tutorial:
.value('Data', function(){
return {message:"I am data from a service"};
})
The Data.message is bound to an input field. When I start the page, there is nothing in the input fields. If I change value to factory, the default message appears in the input field.
Here is the contoller:
controller('FirstCtrl', ['$scope','Data',function($scope, Data) {
$scope.data = Data;
console.log('exiting first controller');
}])
and the index file:
<div ng-controller="FirstCtrl">
<input type="text" ng-model="data.message">
<h1>{{data.message}}</h1>
</div>
Why is the page blank when using value? My assumption is that value is not calculated or computed when the app starts whereas factory is?
Also, where can I find some documentation on $provide? Thank you all.
Set the value to an object, rather than a function:
app.value('Data', {message:"I am data from a service"});
Plunker
See also provide.value(), and this video about $provide (value, constant, service, factory, decorator, provider)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With