0

ファクトリを作成し、各コントローラーでクロスルートを使用しようとしていますが、どうやら何か間違っているようです...

アプリ:

var app = angular.module('sam', ['ngRoute', 'ngGrid', 'ui.bootstrap']);

工場

app.factory("User",function(){
        return {};
    });

ルート

// configure our routes
app.config(function($routeProvider) {
        $routeProvider

            // route for the main page which will direct to the buildings page
            .when('/', {
                templateUrl : 'web/pages/buildings.html',
                controller  : 'mainController',
                controllerAs : 'buildings'
            })

    }); 

コントローラー

app.controller('mainController', ['$filter', '$http','$log', function($filter, $http, $log, User){
        $log.log('hello!!!!!', User);
}]);

これは次を出力します:hello!!!!! undefined

4

3 に答える 3

2

コントローラーに「ユーザー」がありません。

app.controller('mainController', ['$filter', '$http','$log', **'User',** function($filter, $http, $log, User){
        $log.log('hello!!!!!', User);
}]);
于 2014-07-14T15:40:40.690 に答える
1

Userインジェクション配列の一部として含めるのを忘れました

controller('mainController', ['$filter', '$http','$log', function($filter, $http, $log, User){

次のようにする必要があります。

controller('mainController', ['$filter', '$http','$log','User', function($filter, $http, $log, User){
于 2014-07-14T15:40:58.007 に答える
0
app.factory("User",function(){
    return {
           show:function(){
                alert('Factory called')
           }
};
});




app.controller('mainController', ['$filter', '$http','$log','User', function($filter, $http, $log, User){

  //that is the way you can call your factory
  // you can call this factory function in any controller by injecting it.

      User.show(); //will popup alert window.

}]);

于 2014-07-14T15:58:47.067 に答える