Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Angularjs Scope

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 254
    Comment on it

    In AngularJS, Scope is a javascript object by which we can join our controller with the views. In scope we will contain model data which we can access by using $scope object. The use of $scope is easily explained by the simple example.

    Example:

    <html>
    
       <head>
          <title>Angular JS Scope</title>
          <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
       </head>
       
       <body>
          <h2>AngularJS Scope Use</h2>
          
          <div ng-app = "mainApp" ng-controller = "CalcController">
             <p>Enter a number: <input type = "number" ng-model = "number" /></p>
             <button ng-click = "square()">X<sup>2</sup></button>
             <p>Result: {{result}}</p>
          </div>
          
          <script>
             var mainApp = angular.module("mainApp", []);
             
             mainApp.factory('MathService', function() {
                var factory = {};
                
                factory.multiply = function(a, b) {
                   return a * b
                }
                return factory;
             });
             
             mainApp.service('CalcService', function(MathService){
                this.square = function(a) {
                   return MathService.multiply(a,a);
                }
             });
             
             mainApp.controller('CalcController', function($scope, CalcService) {
                $scope.square = function() {
                   $scope.result = CalcService.square($scope.number);
                }
             });
          </script>
          
       </body>
    </html>

     

    Here in the example we can see that we will pass $scope as first argument to controller during its constructor definition. Here $scope.result are the model which are to be used in the view.

    Hence we have used function in $scope. So by this way we can use AngularJS Scope.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: