I am new Angular JS, what i confused is about the scope, I understood what it is and its usage, but i didnot understood why we need it when we have class variables in the scope of the controller.
I mean for example:
angular.module('invoice1', [])
.controller('InvoiceController', function() {
this.qty = 1;
});
<div ng-app="invoice1" ng-controller="InvoiceController as invoice">
Quantity: <input type="number" min="0" ng-model="invoice.qty" required >
</div>
Now, same code (i mean variable qty) using the scope object as follows:
angular.module('invoice1', [])
.controller('InvoiceController',['$scope', function() {
$scope.qty = 1;
}]);
<div ng-app="invoice1" ng-controller="InvoiceController as invoice">
Quantity: <input type="number" min="0" ng-model="qty" required >
</div>
If you see from both the above code snippets, we can access the variable "qty" with in the scope of the div tag. So, why cannot we use the class variables directly, why we have to inject special factory service like scope.
I am sure, their might be reason for this as they created separate service for scope. Please let me know.
The duplicate question answers is good, but too complicated. Actually, i got the answer, inheritance of scope and objects works well with the use of the scope object. I mean, for example:
<!doctype html>
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
angular.module('main', [])
.controller('ParentController',function() {
this.qty = 999;
})
.controller('ChildController', function () {
});
</script>
</head>
<body>
<div ng-app="main">
<div ng-controller="ParentController as parent">
<div ng-controller="ChildController as child">
child exp: {{ child.qty }}
</div>
<b>Invoice:</b>
<div>
Quantity: <input type="number" min="0" ng-model="parent.qty" required >
exp: {{ parent.qty }}
</div>
</div>
</div>
</body>
</html>
Now, if you observe, in the child controller their is no variable called as qty, so simply it cannot access the parent controller qty variable. Now, replace this with the scope as follows:
<!doctype html>
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
angular.module('main', [])
.controller('ParentController',function($scope) {
$scope.qty = 999;
})
.controller('ChildController', function ($scope) {
});
</script>
</head>
<body>
<div ng-app="main">
<div ng-controller="ParentController as parent">
<div ng-controller="ChildController as child">
child exp: {{ qty }}
</div>
<b>Invoice:</b>
<div>
Quantity: <input type="number" min="0" ng-model="qty" required >
exp: {{ qty }}
</div>
</div>
</div>
</body>
</html>
Now, even the child controller does not have qty variable it can inherit from the parent controller and executes the expression "child exp: {{ qty }}" perfectly.
Please any more special usage of the scope? Also, please consider that, i will remove this question or accept my question is answered, if any one feel it as duplicate. As a newbie, i just want the difference with some examples.