Angularjs + Requirejs错误与过滤器

时间:2014-03-25 11:57:58

标签: angularjs requirejs

我尝试在视图中添加带有requirejs的angularjs过滤器,但收到错误:

错误:$ injector:unpr未知提供商

http://docs.angularjs.org/error/$injector/unpr?p0=localizationFilterProvider%20%3C-%20localizationFilter

什么错了?

我的档案:

的index.html

<!DOCTYPE html>
<html>
    <head>
        <script data-main="/static/js/application/main" src="/static/js/libs/require.min.js"></script>
    </head>

    <body>
        <div class="page" ng-view></div>
    </body>
</html>

app.js

'use strict';                                                                                            

define(                                                                                                  
    [                                                                                                    
        'angularAMD',                                                                                    
        'angular-route',                                                                                 
        'angular-animate'                                                                                
    ],                                                                                                   
function (angularAMD) {                                                                                  

    var app = angular.module('FilmOrder', ['ngRoute', 'ngAnimate']);                                     

    app.config(['$routeProvider', function($routeProvider) {                                             

        $routeProvider                                                                                   
            .when('/',                                                                                   
                angularAMD.route({                                                                       
                    templateUrl: 'static/js/application/views/success.html',                             
                    controllerUrl: 'application/controllers/Success',                                    
                    controller: 'Success'                                                                
                })                                                                                       
            )                                                                                            

            .otherwise({redirectTo: '/'});                                                               
    }]);                                                                                                 

    angularAMD.bootstrap(app);                                                                           

    return app;                                                                                          
});             

main.js

require.config({

    baseUrl: "static/js",

    paths: {
        'angular':          'libs/angular.min',
        'angular-route':    'libs/angular-route.min',
        'angular-animate':  'libs/angular-animate.min',
        'angularAMD':       'libs/angularAMD.min'
    },

    shim: {
        'angularAMD': ['angular'],
        'angular-route': ['angular'],
        'angular-animate': ['angular']
    },

    deps: ['application/app']
});

视图/ success.html

<div class="success">
    <div class="success_head">
        {{"Пожалуйста, убедитесь в правильности указанных данных." | localization:'index'}}
    </div>
</div>

过滤器/ localization.js

'use strict'                                           

define(['application/app'], function (app) {           

    app.filter('localization', function () {              
        return 'test';                                       
    });                                                   
});        

controllers / Success.js

define(                                                                                   
    [                                                                                     
        'application/app',                                                                
        'application/filters/localization',                                               
        'application/services/Application'                                                
    ],                                                                                    
    function (app) {                                                                      

    'use strict';                                                                            
    app.register.controller('Success', function ($scope) {                                            

        var Success = {};                                                                       
        $scope.Success = Success;                                                               
    });                                                                                      
});                                                                                       

1 个答案:

答案 0 :(得分:0)

您的过滤器在引导后调用,因此您应该使用app.register:

过滤器/ localization.js

define(['application/app'], function (app) {           
    'use strict'
    app.register.filter('localization', function () {              
        return 'test';                                       
    });                                                   
});

如果这没有帮助,请设置一个plunker,我会尝试进一步帮助。