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

<body>

    <div ng-app="myApp" ng-controller="myCtrl" id='ang'>
        a=<input ng-model="a">
        <div>{{a}}</div>
        <hr>
        b=<input ng-model="b">
        <div>{{b}}</div>
        <hr>
        <div>{{s}}</div>
        <button onclick="sum()">calc</button>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            $scope.a = 10;
            $scope.b = 20;
            $scope.s = Number($scope.a) + Number($scope.b);
        });


        var sum = function () {
            //var scope=angular.element($("#ang")).scope();
            var scope = document.getElementById("ang");
            scope.$apply(function () {
                scope.a = 444;
                scope.b = 222;
                scope.s = 111;
            })
        }
    </script>

</body>

</html>

i am trying to access the scope of angular outside controller and modify the value,but its not working.can we get the scope using document.getElement as we can get using angular.element? please have a look at it and help me out

3
  • You either need to write the function inside the controller with $scope Commented Mar 14, 2017 at 5:14
  • i don't wanna change inside controller Commented Mar 14, 2017 at 5:18
  • Are you going to use a mixed content? Commented Mar 14, 2017 at 5:22

2 Answers 2

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

<body>

    <div ng-app="myApp" ng-controller="myCtrl" id='ang'>
        a=<input ng-model="a">
        <div>{{a}}</div>
        <hr>
        b=<input ng-model="b">
        <div>{{b}}</div>
        <hr>
        <div>{{s}}</div>
        <button ng-click="sum()">calc</button>

    </div>

    <script>
        var app = angular.module('myApp', []);
        app.controller('myCtrl', function ($scope) {
            $scope.a = 10;
            $scope.b = 20;
            $scope.s = Number($scope.a) + Number($scope.b);

       $scope.sum = function() {


                $scope.a = 444;
                $scope.b = 222;
                $scope.s = 111;

       }
        });



    </script>

</body>

</html>

why you want to access outside controller.?? try this code

Sign up to request clarification or add additional context in comments.

1 Comment

i know to change the values inside the controller,but i want a way to get and change the values outside controller
0

Try this code

var demo = angular.module('demo', []);
demo.controller('myCtrl', function ($scope) {
  $scope.a = 10;
  $scope.b = 20;
  $scope.s = Number($scope.a) + Number($scope.b);
});


var sum = function () {
    var scope = angular.element($("#ang")).scope();
    scope.$apply(function(){
    scope.a = 444;
    scope.b = 222;
    scope.s = 111;
    })
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>
<div ng-app='demo'>
    <div ng-controller="myCtrl" id='ang'>
        a=<input ng-model="a">
        <div>{{a}}</div>
        <hr>
        b=<input ng-model="b">
        <div>{{b}}</div>
        <hr>
        <div>{{s}}</div>
        <button onclick="sum()">calc</button>

    </div>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.