0

AngulaR 解決 API

APIは解決のために言います:

key – {string}: コントローラーに注入される依存関係の名前。

@egghead、トピックに関するこのビデオがあります:

Egghead - 角度解決

私が理解していないのは、そのキーオブジェクトの目的と、上記のビデオの作成者がコントローラーをそれ自体に挿入する理由です

4

1 に答える 1

2

key – {string}: コントローラーに注入される依存関係の名前。

app.config(function($routeProvider) {
    $routeProvider.
      when('/', {
        controller: 'ListCtrl',
        resolve: {
          myResolve: function(MyService) {
            return MyService();
          }
        },
        templateUrl:'./views/list.html'
  })
});

代わりに(コントローラー内)

app.controller('MyController',function($scope,MyService){
   $scope.data =  MyService();    
});

解決を使用する場合

app.controller('MyController',function($scope,myResolve){
    $scope.data =  myResolve;      
});

アップデート

実際の例

<!doctype html>
 <html ng-app="myModule">
    <head>
        <meta charset="utf-8">

    </head>
    <body>
        <div id="content" data-ng-view=""></div>
        <script src="http://code.angularjs.org/1.0.8/angular.min.js"></script>
        <script>
            var myModule = angular.module('myModule', []);
            myModule.config(function ($routeProvider) {
                $routeProvider
                    .when('/', {
                        templateUrl: './index.html',
                        controller: 'IndexCtrl',
                        resolve: {
                            hello: function(Hello) {
                                return Hello.getMessages();
                            }
                        }
                    })
                    .otherwise({
                        redirectTo: '/'
                });

            });
            myModule.factory('Hello', function($q, $timeout) {
                var getMessages = function() {
                    var deferred = $q.defer();
                    $timeout(function() {
                        deferred.resolve('Hello');

                    }, 1000);

                    return deferred.promise;
                };
                return {
                    getMessages: getMessages
                };
            });
            myModule.controller('IndexCtrl',function($scope,hello){
               $scope.hello =  hello;
            });
        </script>
    </body>
</html>

景色

<p>{{hello}}</p>
于 2013-10-10T12:09:29.080 に答える