Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs value vs factory

Tags:

angularjs

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.

like image 393
U-L Avatar asked Apr 15 '13 23:04

U-L


1 Answers

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)

like image 81
Mark Rajcok Avatar answered Oct 02 '22 07:10

Mark Rajcok