ca2264
ca2264

Reputation: 1

$scope and $rootScope in Angular JS

I am working on ionic project , I have 2 views first view : The user enter a value (integer number) then click a button to take me to view 2 Second view : Displays to me list of numbers from 1 to the value the user entered

this is my input :

<input type="text" placeholder="input a value " ng-model="val">

and this is the Ctrl for the view 1

.controller('homeCtrl', function($rootScope, $scope) {
$scope.val;
$rootScope.value= $scope.val;
})

this is view 2 list

<ion-list>
    <ion-item ng-repeat='num in list' item="num"> {{num}} 
    </ion-item>
</ion-list>

and this is the ctrl

.controller('listCtrl', function($rootScope, $scope) {

var num = $rootScope.value;
$scope.list=[];
  for(var i=1; i<num ; i++)
  {
    $scope.list.push(i);
  }

})

the problem is in the first controller , this line

$rootScope.value= $scope.val;

the $rootScope doesn't take the value of $scope.val when I put a test value like this $rootScope.value= 8;

the list are working, else it doesn't

where are the problem please I don't have any errors thanks

Upvotes: 0

Views: 102

Answers (1)

Robba
Robba

Reputation: 8324

You're assigning the current value of $scope.val

What you'll need to do is add a $watch to watch for changes to 'val' like so:

$scope.$watch('val', function(newval) {
  $rootScope.value = newval;
});

Upvotes: 2

Related Questions