2013-02-26 16 views
8

requirejsモジュールを使用してディレクティブを要求し定義する方法が正確にはわかりません。requirejsモジュールでangularjs指令を定義するにはどうすればよいですか?

これは、これは私のmain.jsにコードされディレクティブディレクティブ/ locationBtn.jsを含むファイル

define(['Zf2NVIApp'], function (Zf2NVIApp) { 
    'use strict'; 

    Zf2NVIApp.directive('locationBtn', function() { 
     return { 
      template: '<div></div>', 
      restrict: 'E', 
      link: function postLink(scope, element, attrs) { 
       console.log("we are in the location btn module"); 
       element.text('this is the locationBtn directive'); 
      } 
     }; 
    }); 

}); 

ための私のコードでは、あなたが近くにいる

require.config({ 
shim: { 
}, 

paths: { 
    angular: 'vendor/angular', 
    jquery: 'vendor/jquery.min', 
    locationBtn: 'directives/locationBtn' 
} 
}); 

require(['Zf2NVIApp', 'locationBtn'], function (app, locationBtn) { 
// use app here 
angular.bootstrap(document,['Zf2NVIApp']); 
}); 

答えて

12

を提出。あなたの「Zf2NVIApp.js」ファイルは、あなただけのディレクティブAMDモジュール定義に値を返すために必要以上

define(['angular'], function(angular){ 
    return angular.module('Zf2NVIApp', []); 
}); 

が含まれていることを考えると、それが動作するはずです:

define(['Zf2NVIApp'], function (Zf2NVIApp) { 
    'use strict'; 

    Zf2NVIApp.directive('locationBtn', function() { 
    return { 
     template: '<div></div>', 
     restrict: 'E', 
     link: function postLink(scope, element, attrs) { 
     console.log("we are in the location btn module"); 
     element.text('this is the locationBtn directive'); 
     } 
    }; 
    }); 

    // You need to return something from this factory function 
    return Zf2NVIApp; 

}); 
+0

ええそれはトリックをしました。 –

関連する問題