2017-05-17 4 views
-1

Unten ist meine Definition von Controller. Wenn ich die Anwendung starte, bekomme ich

"$ rootScope ist nicht definiert".

Kann jemand mir helfen zu verstehen, was hier falsch ist.

var webadmin = angular.module('PcPortal') 
    .controller('environmentController', 
    [ 
     '$scope', '$http', '$rootScope', 
     function ($scope, $http) { 
      $http.get('http://localhost:52240/api/PcpEnvironment/GetAllEnvironments') 
       .then(function (response) { 
        $scope.Environments = response.data; 
       }); 

      $scope.findSelectedItem = function (index) { 
       var env = $scope.Environments[index]; 
       $rootScope.selectedEnv = env; 
      }; 

     } 
    ]); 

Html Linie Funktion aufzurufen ...

<td><a ng-click="findSelectedItem($index)" href="#/UpdateEnvironment">Update</a></td> 
+3

Fehler ist klar. 'function ($ scope, $ http, $ rootScope)' – Satpal

+0

Sie vermissen den '$ rootScope' Parameter in Ihrer Controller Funktion ... – Cerbrus

+0

Fügen Sie einfach $ rootScope zur Funktion hinzu ($ scope, $ http, $ rootScope) .. ........... –

Antwort

0

Sie injizieren $rootScope, aber sie schaffen ihre Instanz als $http

die Sie interessieren und $http Instanz als $http und $rootScope Instanz als $rootScope

erstellen
var webadmin = angular.module('PcPortal') 
    .controller('environmentController', 
    [ 
     '$scope', '$http', '$rootScope', 
     function ($scope, $http, $rootScope) { 
      $http.get('http://localhost:52240/api/PcpEnvironment/GetAllEnvironments') 
       .then(function (response) { 
        $scope.Environments = response.data; 
       }); 

      $scope.findSelectedItem = function (index) { 
       var env = $scope.Environments[index]; 
       $rootScope.selectedEnv = env; 
      }; 

     } 
    ]); 
+0

Bekam ... es funktionierte perfekt, wie ich es wollte ... Vielen Dank Können Sie erklären, warum ich '$ http' zweimal brauche .... – Abhash786

+1

@ Abhash786: Sie sollten nicht "$ http", "$ http" verwenden. Das ist dumm. Diese Antwort ist falsch, da Ihr $ rootScope nicht richtig funktioniert. – Cerbrus

+0

@Cerbrus ja, das war wirklich albern, das war Tippfehler jetzt überprüfen, :) –

Verwandte Themen