0

As per mentioned, Is it possible to assign $scope variable from ng-init to new variable?

see this plunkr

If you see on my plunker, variable $scope.adulttotTicket have value of total ticket which is 13. I create new variable using ng-init called totTicket and assign it to $scope.adulttotTicket

<p data-ng-init="totTicket = $scope.adulttotTicket"></p>

but when I output value totTicket, I got value 0

totTicket: {{ totTicket }}

#note point of this question is, I want to assign new variable with value $scope variable

Anyone have any Idea would appreciate, thanks!

Fai Zal Dong
  • 323
  • 6
  • 25

2 Answers2

0

It should be,

<p data-ng-init="totTicket = adulttotTicket"></p>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • no luck, got no value. see this plunker [https://plnkr.co/edit/21mGfltfkkHaOoLaGx3Q?p=preview] – Fai Zal Dong Feb 14 '17 at 06:59
  • @FaiZalDong check this https://plnkr.co/edit/k73hMYGuFYp8kag8va0V?p=preview – Sajeetharan Feb 14 '17 at 07:02
  • change to whatever value you want https://plnkr.co/edit/k73hMYGuFYp8kag8va0V?p=preview – Sajeetharan Feb 14 '17 at 07:03
  • please do not change any value. you just change value `adulttotTicket` to `10`..... – Fai Zal Dong Feb 14 '17 at 07:05
  • what are you trying to do? – Sajeetharan Feb 14 '17 at 07:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135652/discussion-between-sajeetharan-and-fai-zal-dong). – Sajeetharan Feb 14 '17 at 07:08
  • you just change this `$scope.adulttotTicket = 0">` to `adulttotTicket = 10">`. this variable `$scope.adulttotTicket` is important for me, do not change this value. What I need is, I want `totTicket` equal to this `$scope.adulttotTicket`. see this plunker [https://plnkr.co/edit/21mGfltfkkHaOoLaGx3Q?p=preview] – Fai Zal Dong Feb 14 '17 at 07:08
  • you should not use $scope in the template – Sajeetharan Feb 14 '17 at 07:10
  • If you see on `ng-repeat`, Im using this variable `$scope.adulttotTicket`, If you just simply change value `adulttoTicket` to `10`, you just ruined the value `+` inside `ng-repeat` – Fai Zal Dong Feb 14 '17 at 07:10
0

You got 0 value in totTicket because ng-init happens only once when the scope created when $scope.adulttotTicket equals 0 ,while the "adulttotTicket" ng-init is happening every iteration of the ng-repeat. Add some condition to <p data-ng-init="totTicket = $scope.adulttotTicket">

or change the assignment of the totTicket in the scope with $watch or something.
aslavkin
  • 214
  • 1
  • 7
  • for now `$scope.adulttotTicket` have value `13`. Can we just simply create new variable and assign new variable to `$scope.adulttotTicket` ? – Fai Zal Dong Feb 14 '17 at 07:43
  • http://stackoverflow.com/questions/25574048/does-ng-init-watch-over-change-on-instantiated-property-like-ng-model-does – aslavkin Feb 14 '17 at 11:29