2017-03-26 38 views
1

即時嘗試將解析\承諾添加到我的項目中,所以當您請求一個頁面時,它只會在接收到來自服務器。 這是我的js代碼:

'use strict'; 

angular.module('myApp.show', ['ngRoute']) 

.config(['$routeProvider', function ($routeProvider) { 
$routeProvider.when('/show', { 
    templateUrl: 'views/show.html', 
    controller: 'showCtrl', 
    resolve: { 
     booksList: function ($http) { 
      return ($http.get('data/books.json') 
       .then(function (data) { 
        return data; 
       })); 
     } 
    } 
    }); 
}]) 

.controller('showCtrl', ['booksList', '$scope', function (booksList, $scope) { 
    $scope.books = booksList; 
    $scope.removeRow = function (productIndex) { 
     if (window.confirm("are you sure?")) { 
      $scope.books.splice(productIndex, 1); 
    } 
    } 
}]) 

,但是這是我得到: 錯誤:[$注射器:unpr]未知提供商:booksListProvider < - booksList < - showCtrl

我還挺新的角度,但我遵循了幾個教程,同時它在視頻中工作 - 我不斷收到錯誤。

HTML:

<div class="table-responsive"> 
    <div ng-app="myApp.show" ng-controller="showCtrl"> <!-- ctrl --> 
     <table st-table="rowCollection" class="table table-striped"> 
      <thead> 
       <tr> 
        <th st-sort="author">Author</th> 
        <th st-sort="date">Date</th> 
        <th st-sort="title">Title</th> 
        <th></th> 
       </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="(bookIndex, book) in books"> 
       <td class="col-md-3">{{ book.author }}</td> 
       <td class="col-md-3">{{ book.date | date }}</td> 
       <td class="col-md-4">{{ book.title | beutifyTitle }}</td> 
       <td class="col-md-1"><ng-include src="'views/partials/editBook.html'"></ng-include></td> 
       <td class="col-md-1"><button class="btn btn-warning" ng-click="removeRow()">Delete</button></td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</div 
+0

我期望使用ng-controller加載控制器將無法與路由器....該解決方案需要通過ngRoute注入 - 我只有UI-Router的經驗,雖然 – Daniel

回答

0

您應該從模板中移除ng-controller="showCtrl"。原因是,你已經通過路由器分配showCtrl。但是,由於您再次在內聯模板中寫過ng-controller,因此無法獲得booksList解決方案提供程序。

+0

哇!謝謝!!我在這個問題上花了很多時間..沒有意識到它在HTML代碼中,而不是JS ..無論如何,你的幫助非常感謝! – user3328918

+0

@ user3328918謝謝,你可以接受我的答案:) –

相關問題