2

In the code below I log to console the $rootScope and it logs undefined. Why does it happen?

$scope.init = function ($rootScope) {
    // var s = $cookies.get("arr");
    // var ex = [];

    var ex = $cookies.get("arr");
    $scope.arry = JSON.parse(ex);
    alert("scooe.arry" + $scope.arry);
    console.log("JSON.stringify($cookies.get('arr'))=" + $scope.arry);

    //create a call to google api for all stocks in the $scope.arry
    $scope.arry.forEach(function(entry) {
        console.log("entry is " + entry);
        var v = httpcall.getVal(entry);
        $scope.arry.push(v);
        //console.log("rootscoooope =" + $rootScope.share + "==");
        console.log($rootScope); debugger;
    });

The init function is called by ng-init of a controller called NameCtrl.

<ul>
    <li ng-repeat="sr in arry track by $index"> value : {{sr.l_cur}}
         <a href='#' onhover="remove" ng-click="removename(sr)"> (-)</a>
    </li> 
</ul>
Liron Ilayev
  • 386
  • 4
  • 19
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41

2 Answers2

1

As you can read in angular documentation for ngInit

(...) There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope.

So you rather should prefer place your logic directly into your controller. And there should not be any problem to inject root scope into controller.

1

$rootScope in your example is an argument. How do you call init function ?

From template you should call it init($root)

Valery Kozlov
  • 1,557
  • 2
  • 11
  • 19