Friday, February 19, 2016

AngularJS Scope ($scope)

Scope is bridge between Html(View) and controller in Angular JS applications. It is a java script object holding properties and method for view and controller.

The scoped defined at application level is called $rootScope and is available in entire application.

If we two variable with same names in root scope and current scope then current scope variable will get precedence.

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="myApp">

RootScope : {{myVar}}

<div ng-controller="myCtrl">

Controller Scope : {{myVar}}

</div>

<script>

var app = angular.module('myApp', []);
app.run(function($rootScope) {
    $rootScope.myVar = 'In Root Scope';
});
app.controller('myCtrl', function($scope) {
    $scope.myVar = "In current contrller scope";
});

</script>

</body>
</html>



No comments:

Post a Comment