simple color example with angular js

37 views
Skip to first unread message

masterinex

unread,
Oct 31, 2016, 5:09:25 PM10/31/16
to Angular
I changed rootscope to scope for the first controller. Now it won't list the colors. 

Shoudn't it be:


Blue

Red

Red


after i made the changes ? 




<!DOCTYPE html>
<html>
<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>

Sander Elias

unread,
Nov 1, 2016, 1:55:13 AM11/1/16
to Angular
Hi materinex,

Stop doing that course! You should never-ever-never use $rootScope unless you are very versed in angular 1, and have deep knowledge about what you are doing with it. Also, you should not be using $scope either by now. Use the new constructs like .components and so on.  
From the looks of it this course is very dated, and not up to date with current affairs. Techniques that are displayed in there are now frowned upon.

To learn angular 1 you are better off reading the styleguide, and the tutorial combined with the official docs.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages