2013-02-26 90 views
8

我不確定如何使用requirejs模塊請求和定義指令。我們如何在requirejs模塊中定義一個angularjs指令?

這是我對含有指令的指令文件的代碼/ 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'); 
      } 
     }; 
    }); 

}); 

這是代碼我main.js文件

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」文件包含

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

比你只需要在你的指令AMD模塊定義返回值,它應該工作:

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

是的,這確實起作用。 – 2013-02-26 21:36:47

相關問題