Ravi K Chowdary February 2016

facing issue using $scope and $rootscope

I am facing issue to print text using angualrjs $scope and $rootscope. Please find the code below and let me know where i did mistake.

<body ng-app="x">

        <div ng-controller="ctrl1">
            Hello {{name2}}
        </div>

        <div ng-controller="ctrl2">
            Hi {{name1}} How {{name3}}
        </div>

        <script>

            var app= angulr.module("x",[]);

            app.controller("ctrl1" ,['$scope', '$rootScope',function($scope, $rootscope){

                $scope.name2 = "Sir";
                $rootscope.name3 = "are you!";
            }]);

            app.controller("ctrl2", function($scope){

                $scope.name1 = "madam";
            });

        </script>

    </body>

Answers


Shubham Dubey February 2016

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

<div ng-controller="ctrl1">
        Hello {{name2}}
    </div>
    <div ng-controller="ctrl2">
        Hi {{name1}} How {{name3}}
    </div>

<script>
var app = angular.module('x', []);
 app.controller("ctrl1" ,['$scope', '$rootScope',function($scope, $rootscope){

            $scope.name2 = "Sir";
            $rootscope.name3 = "are you!";
        }]);
app.controller("ctrl2", function($scope){

            $scope.name1 = "madam";
        });

</script>

</body>
</html>


pritesh February 2016

Please run the following code. Problem solved. You spelled angular incorrectly and you dont need to put scope and rootScope in bracket in ctrl1 in controller. Also it is rootScope and not rootscope (check the capital S)

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<body ng-app="x">

        <div ng-controller="ctrl1">
            Hello {{name2}}
        </div>

        <div ng-controller="ctrl2">
            Hi {{name1}} How {{name3}}
        </div>

        <script>

            var app= angular.module("x",[]);

            app.controller("ctrl1" ,function($scope, $rootScope){

                $scope.name2 = "Sir";
                $rootScope.name3 = "are you!";
            });

            app.controller("ctrl2", function($scope){

                $scope.name1 = "madam";
            });

        </script>

    </body>

Post Status

Asked in February 2016
Viewed 1,459 times
Voted 12
Answered 2 times

Search




Leave an answer