3

AMD (require.js) を使用するボイラー Angular アプリケーションを準備しています。これまでのところ機能していますが、サービスに問題があります。サービスを組み込むにはどうすればよいですか?

これは Main.js です。

require.config({  

paths: {
// These are the core components - without which our application could not run
angular: '../lib/angular/angular',
angularResource: '../lib/angular/angular-resource',

// These are components that extend our core components
jquery: '../lib/jquery/jquery-1.8.2.min',
bootstrap: '../lib/bootstrap/js/bootstrap',
underscore: '../lib/underscore/underscore',

text: '../lib/require/text'  

},
  shim: {
    'angular' : {'exports' : 'angular'},
    'angular-resource' : {deps:['angular']},
    'bootstrap': {deps:['jquery']},
    'underscore': {exports: '_'}
  },
  priority: [
    "angular"
  ],
  urlArgs: 'v=1.1'
});

require( [
  'angular',
  'app',
  'services/services',
  'services/dateGetter',
  'controllers/controllers',
  'controllers/navbar',
  'controllers/exampleTemplateController',
  'filters/filters',
  'directives/directives',
  'routes'
], function(angular, app) {

  angular.element(document).ready(function () {
    angular.bootstrap(document, ['myApp']);
  });
});

これが私がコントローラーを開発した方法です:

define([
    'app',
    '../helpers/exampleFile'  //example of importing custom file.js in our controller.
],

function(app, exampleFile) {

    'use strict';
    return app.controller('exampleTemplateController', [
        '$scope', 'dateGetter',
        function ($scope ) {
            $scope.sayHello = function(module) {

              alert("Current Date: " + dateGetter.getCustomDate + " " + exampleFile(module.name));

            }
        }
    ]);
});

これは私がサービスを開発しようとした方法です(ここでは惨めに失敗しています):

 define([
    'app'
],

function(app) {
        'use strict';
                debugger;
        return app.factory('dateGetter', [
            '$scope',
            function ($scope ) {
                $scope.getCustomName = function() {
                    return "THIS IS MY NAME";
                }
            }
        ]);
    });

私は近くにいると思いますが、何かが私を逃しています。

もう 1 つの質問は、もし私がたくさんのコントローラとサービスを持ってしまったら... main.js ファイル (require: の下) にすべての人をリストする必要がありますか?

4

1 に答える 1

2

サービスが動作するようになりました。私の責任です。これで、アクセスして使用できるようになりました。

define([
    'app'
],

function(app) {
    return app.factory('serviceExample', function ( ) {

          return {
              nameOfMethod: function () {
                  return "Is this working";
              }
          }
        }
    );
});

私が今疑問に思っていること... Angularでrequiredを使用する必要はありますか?

簡潔な答えなしで、同じ質問について議論している多くの投稿を読みました。以前使用していたので、require を使用することを考えていましたが、このアプリケーションはいずれにせよ大きなバグになるでしょう。

意見?

于 2013-11-01T09:04:12.093 に答える