Reputation:
I know AngularJS has some Dependency Injection techniques. So if the code is and mainApp.value("defaultInput", 5); passes the data to the controller.
//define a module
var mainApp = angular.module("mainApp", []);
//create a value object as "defaultInput" and pass it a data.
mainApp.value("defaultInput", 5);
...
//inject the value in the controller using its name "defaultInput"
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
$scope.number = defaultInput;
$scope.result = CalcService.square($scope.number);
$scope.square = function() {
$scope.result = CalcService.square($scope.number);
}
});
Why not just declare var defaultInput = 5 before the model definition? Why create a new syntactical sugar in AngularJS with value? Why create totally something new when you already have an option? Why complicate things?
Upvotes: 0
Views: 26
Reputation: 222682
Yes you can do that, the difference is that,
if you use var, the context is available only to the particular controller. if you want to declare in another controller you need to use another var and assign the value.
When you use .value, it behaves as a global constant , so whenever you need to use the value, you can just inject the value and use across controllers.
Upvotes: 1