Wednesday, 13 September 2017

value object in angular JS

value in angular js :

Value in angular JS is an object. By using this object we can create or construct the values that which can be used in the dependent controllers.We can also say it as like "providing the service for the dependents" where the dependents tends to the controllers.

Look of value :
 var app = angular.module('app', []);
     app.value('valueis',20);

From the above we can observe that the module "app" is set to dbe registered with the "value" object with holding the "valueis" variable with its value 20.
In value object we can define any type of data like string, int, array, object.

Look of value with object :
var app = angular.module('app', []);
 app.value('user', {
            userid: 1,
            username: 'Hari',
            usersal: 20000
        });
 app.controller('appCtr2', function ($scope,user) {
            $scope.name= user.username;
        });

<div ng-controller="appCtr2">
      User Name is : {{name}}
    </div>

We can also access this value objects data in multiple controllers as it will instatiate by the time of angular js configuring within browser.
while equalizing any other object to the "Value" object, a direct mapping is not allowed as like follows :
Let us assume the same properties of "User" in "response" object.
    user = response     --> Cannot be applied.
    user.Id  = response.Id      --> can be applied.
    user.name= response.name  --> can be applied.

We can even change the value of the "Value" object which will not prompt any error and applies whereever we use, as the angularjs supports the two-way data binding.

No comments:

Post a Comment