2

As mentioned on title, I have small issue to assign new variable with $rootScope variable.

I have variable $rootScope.adulttotTicket with value 2. the value is int. So what I want is just to assign new variable newVal to $rootScope.adulttotTicket variable.

<p data-ng-init="newVal = $rootScope.adulttotTicket"></p>

but when I output variable newVal, it show nothing on my browser. So I try to edit at plunker, so I found out the value newVal is undefined. I don't have any idea what to do here.

newVal should easily have value 2 from $rootScope.adulttotTicket. but I got nothing. see on this plunkr plunkr

Hope get idea from anyone. thanks !

4
  • Just wanted to mention that what you are trying to do sounds like a bad idea - as we all know, gobal state is evil and ng-inits are too. Not saying there are no cases where its needed, but it should generally be a sign to rethink your design. Commented Feb 10, 2017 at 10:36
  • You should use controllers rather than ngInit to initialize values on a scope. Commented Feb 10, 2017 at 10:38
  • It shows adulttotTicket: 2 newVal: unassigned I guess thats what you need right? Commented Feb 10, 2017 at 10:47
  • We had this strange bug and the only thing work is convert a variable to object. like $rootScope.ticket.adulttotTicket Commented Mar 2, 2021 at 7:16

2 Answers 2

1

Access $rootScope in view by using $root instead:

<p data-ng-init="newVal = $root.adulttotTicket"></p>

I updated your plunkr and it does work fine.

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

4 Comments

still unassigned.. supposedly newVal show value 2
@FaiZalDong I updated the plunker. It does work now but you should not do it like this ...
But my question is to assign new variable to $rootScope.adulttotTicket, eg: <p data-ng-init="newVal = $rootScope.adulttotTicket"></p>. what you showing is you use back the value of $root
see this plunkr [plnkr.co/edit/21mGfltfkkHaOoLaGx3Q?p=preview]. supposed value nv get value $root.newVal @lin
0

When referring to $rootScope in a view use $root instead:

<p data-ng-init="newVal = $root.adulttotTicket"></p>

UPDATE

The reason why the above does not work is because you are not in a scope and so have no $scope to access the $root of! To fix you can declare your variable on the $rootScope in a run block as follows:

angular.module('myModule').
run(function($rootScope) {
    $rootScope.adulttotTicket = 'unassigned';
});

You can then access this in the view (with no reference to $root or $rootScope) as follows:

<p data-ng-init="newVal = adulttotTicket"></p>

4 Comments

can guys show in plunkr? I have tried, but still not working... [plnkr.co/edit/uw3Uj1HmcK2eJYCUlYWj?p=preview]
actually I dont want to use controller, can you see my plunkr
@FaiZalDong I'm not using a controller.
Updated your plunker, not a single controller in sight.

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.