簡體   English   中英

未捕獲的ReferenceError:在SearchController中未定義$ scope

[英]Uncaught ReferenceError: $scope is not defined in SearchController

我對Angular很陌生。 我正在嘗試使用Star Wars API實現搜索頁面,但未定義$ scope。 我不確定這里做錯了什么:

'use strict';
var app = angular.module('starApp');

/* Application controller (for top-level auth) */
app.controller('SearchController', ['$location', 'AuthenticationService',
'$scope', '$http',
function($location, AuthenticationService, $scope, $http) {

    console.log('*** SearchController ***');

    $scope.items = [];

    (function getData() {
        var apiURL = "https://swapi.co/api/planets";
        axios.get(apiURL).then(function(response) {
            showDetail(response.data);
        });
    })();

    function showDetail(data) {
        $scope.items = data.results;
    }

}]);

我正在將$ scope注入到我的控制器中,但是仍然顯示它是未定義的。

您應該使用$ http服務。 這是工作片段:

 'use strict'; var app = angular.module('myApp',[]); /* Application controller (for top-level auth) */ app.controller('SearchController', ['$location', '$scope', '$http', function($location, $scope, $http) { console.log('*** SearchController ***'); $scope.items = []; $scope.loading = false; var apiURL = "https://swapi.co/api/planets"; $scope.loadData = function() { $scope.loading = true; $http.get(apiURL) .then(function(response) { $scope.showDetail(response.data); $scope.loading = false; }); }; $scope.loadData(); $scope.showDetail = function(data) { $scope.items = data.results; }; } ] ); 
 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="SearchController" ng-cloak> <ul ng-if="items.length>0 && !loading" > <li ng-repeat="item in items">{{item.name}}</li> </ul> <div ng-if="loading">Loading...</div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM