Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am following the tutorial

Tryit Editor v3.1[^]



I have changed $rootscope to scope for the first controller . but now it wont list the colors.

Should it be:


Blue

Red

Red , after i made the changes.






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

<p>The rootScope's favorite color:</p>
<h1>{{color}}</h1>

<div ng-controller="myCtrl">

<p>The scope of the controller's favorite color:</p>
<h1>{{color}}</h1>

</div>

<p>The Scope's favorite color is :</p>
<h1>{{color}}</h1>

<script>
var app = angular.module('myApp', []);
app.run(function($scope) {
$scope.color = 'blue';
});
app.controller('myCtrl', function($scope) {
$scope.color = "red";
});
</script>

<p>Notice that controller's color variable does not overwrite the rootScope's color value.</p>

</body>
</html>

What I have tried:

------------------------*************************************************************************************************************************************************************************************
Posted
Updated 3-Nov-16 17:34pm

1 solution

you should use $rootScope object
JavaScript
app.run(function ($rootScope) {
           $rootScope.color = 'blue';
       });
 
Share this answer
 
Comments
forte74 4-Nov-16 2:27am    
how come it does not work without the root ie scope.color = 'blue'
Karthik_Mahalingam 4-Nov-16 4:09am    
because the $scope object is available inside controller only

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900