-1

First of all excuse me if this doesn't make any sense.

I have a root-scope with two different values assigned in the same controller,and now i want to print those two values using that root-scope.....how can i achieve this

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,$rootScope) {
  $rootScope.name = 'hello';
  $rootScope.name="world";
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>

  
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
   
  </head>

  <body ng-controller="MainCtrl">
    <p>{{name}}</p>
  </body>

</html>

from the above i want to print hello world......

Sa E Chowdary
  • 2,075
  • 15
  • 31

2 Answers2

0

You are overwriting the first value when you call $rootScope.name = "world";, I recommend you make an object instead like so;

$rootScope.helloWorld = {hello: "hello", world: "world"};

In the html;

<p>{{helloWord.hello}} {{helloWorld.world}}</p>
Tom Johnson
  • 679
  • 5
  • 15
0

What you want to do is

$rootScope.name = "hello";
$rootScope.name += " world";

In your code, you are just replacing the value.

Roux
  • 293
  • 2
  • 17
  • may be your correct....surely its like replacing i missed that but lets see do we have any other choices – Sa E Chowdary Jan 05 '17 at 10:42
  • Well, it's either this solution (which still doesn't make any sense, or just do $rootScope.hello = "hello"; $rootScope.world = "world"; and display them in the view with {{hello}}{{world}} – Roux Jan 05 '17 at 10:45
  • i know that way but a bloody interviewer makes me blank and i am hunting this now – Sa E Chowdary Jan 05 '17 at 10:51