angularjs +下载csv文件+错误:[$ injector:unpr]未知提供者

时间:2016-02-10 10:05:18

标签: javascript angularjs csv

我是angularjs的新手。

我尝试下载csv文件。所以我尝试使用angular sanitize和ng-csv文件。

的index.html

<!DOCTYPE html>
<html lang="en">
    <head>

        <script src="vendor/theme_files/js/jquery.min.js"></script>
    </head>
    <body ng-app="myApp" class="nav-md">

        <div class="container body" ui-view></div>


        <script src="vendor/angular/angular.js"></script>
        <script src="vendor/angular-resource/angular-resource.js"></script>
        <script src="vendor/angular-ui-router/release/angular-ui-router.js"></script>
        <script src="js/services/lb-services.js"></script>
        <script src="vendor/angular-sanitize/angular-sanitize.js"></script>
        <script src="vendor/ng-csv/ng-csv.min.js"></script>

        <script src="js/app.js"></script>
        <script src="js/controllers/offerLetterCtl.js"></script>
        <script src="js/services/offerLetter.js"></script>

    </body>
</html>

app.js

angular.module('myApp', ['lbServices', 'ui.router']).config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
    $stateProvider.state('login', {
        url : '/login',
        templateUrl : 'views/login.html',
        controller : 'loginController'
    }).state('addOfferletter', {
        url : '/create_offerletter',
        templateUrl : 'views/create_offer_letter.html',
        controller : 'offerLetterController'
    });
    $urlRouterProvider.otherwise('login');

}]);

offerLetterCtl.js(控制器文件夹)

angular.module('myApp').controller('offerLetterController', ['$scope', '$location', 'offerLetterService',
    ,function($scope, $location, offerLetterService) { 
        //mycode

}]);

offerLetter.js(服务文件夹)

angular.module('myApp').factory('offerLetterService', ['$q', '$timeout', '$http',
function($q, $timeout, $http,OfferLetter) {

        //mycode

}]);

create_offer_letter.html

<button type="button" ng-csv="getArray()" csv-header="['Field A', 'Field B', 'Field C']" filename="test.csv">
        Export
</button>

我按照此链接进行了csv下载。 enter link description here

如果我在控制器中包含这些行

['ngSanitize', 'ngCsv']


 angular.module('myApp').controller('offerLetterController', ['$scope', '$location', 'offerLetterService',"ngSanitize", "ngCsv"
    ,function($scope, $location, offerLetterService, ngSanitize, ngCsv) {

        $scope.filename = "test";  
}]);

错误详情:

 Error: [$injector:unpr] Unknown provider: ngSanitizeProvider <- ngSanitize <- offerLetterController
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=ngSanitizeProvider%20%3C-%20ngSanitize%20%3C-"<div class="container body ng-scope" ui-view="">"fferLetterController

2 个答案:

答案 0 :(得分:0)

我认为你没有在app.js中将ngSanatize模块添加到你的应用程序中。试试这样:

angular.module('myApp', ['lbServices', 'ui.router', 'ngSanitize']).config(['$stateProvider', '$urlRouterProvider',

function($ stateProvider,$ urlRouterProvider){

这至少是错误的:

angular.module('myApp').controller('offerLetterController', ['$scope', '$location', 'offerLetterService',"ngSanitize", "ngCsv" ,function($scope, $location, offerLetterService) {

你的错误在于你在第一部分中有5项服务,而你只是注入了3.如果你对齐它们会有所帮助:

 angular.module('myApp').controller('offerLetterController', ['$scope', '$location', 'offerLetterService',"ngSanitize", "ngCsv" ,function($scope, $location, offerLetterService, ngSanitize, ngCsv) {

接下来,这也不对:

angular.module('myApp').factory('offerLetterService', ['$q', '$timeout', '$http',
function($q, $timeout, $http,OfferLetter) {

您正在定义名为offerLetterService的工厂,我不想在那里插入它吗?

答案 1 :(得分:0)

从控制器中删除了注射。这应该有效!

angular.module('myApp').controller('offerLetterController', ['$scope', '$location', 'offerLetterService'
    ,function($scope, $location, offerLetterService) {

        $scope.filename = "test";  
}]);
相关问题