$scope is an article information stockpiling extension, or we can comprehend it as an information distribution center which different items as Controller, Service will utilize and trade of data through it.
you can make any sort of variables at once and utilize anyplace inside controller.$scope and $rootscope is spine of AngulerJs.
Basic Example of controller in AngularJs:-
Step 1:-Create simple HTML page.
Step 2:-Include AngularJs library to support Angularjs in your application inside head tag with script tag.
First of all i've created a $scope variable name is"firstName " and set value="Bikesh" and now we are using single controller name is "simpleController " so I able to use "Bikesh" on my HTML Part with ng-model or expression {{firstName}}. But you need to inject $scope in controller.
you can make any sort of variables at once and utilize anyplace inside controller.$scope and $rootscope is spine of AngulerJs.
Basic Example of controller in AngularJs:-
Step 1:-Create simple HTML page.
Step 2:-Include AngularJs library to support Angularjs in your application inside head tag with script tag.
or /use CDN URL:-<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<!DOCTYPE html>Step 3:-Create Controller:-script.js file
<html>
<head>
<script src="angular.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="mainApp">
<div ng-controller="simpleController">
<strong>First name:</strong> {{firstName}}<br />
<strong>Last name:</strong> <span ng-bind="lastName"></span><br />
<strong>Full name:</strong> {{getFullName()}}<br />
<br />
<label>Set the first name: <input type="text" ng-model="firstName"/></label><br />
<label>Set the last name: <input type="text" ng-model="lastName"/></label>
</div>
</body>
</html>
var mainApp = angular.module("mainApp", []);Description:-
mainApp.controller('simpleController', function($scope)
{
// Initialize the model variables
$scope.firstName = "Bikesh";
$scope.lastName = "Srivastava";
// Define utility functions
$scope.getFullName = function ()
{
return $scope.firstName + " " + $scope.lastName;
};
});
First of all i've created a $scope variable name is"firstName " and set value="Bikesh" and now we are using single controller name is "simpleController " so I able to use "Bikesh" on my HTML Part with ng-model or expression {{firstName}}. But you need to inject $scope in controller.
You have just read an article that categorized by title AngularJs
by title What is $scope in controller ?. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/01/what-is-sope-in-controller.html. Thank You!
Author:
Bikesh Srivastava - Tuesday, January 19, 2016
You have written an informative post. Looking forward to read more.
ReplyDeleteResponsive Website Design Services