2013-02-26 23 views
8

Nie jestem dokładnie pewien, jak zażądać i zdefiniować dyrektywę przy użyciu modułu requirejs.Jak definiujemy dyrektywę angularjs w module requirejs?

To jest mój kod do pliku zawierającego wytyczne dyrektywy/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'); 
      } 
     }; 
    }); 

}); 

jest to kod do pliku moi 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']); 
}); 

Odpowiedz

12

Jesteś blisko. Biorąc pod uwagę, że plik swoim „Zf2NVIApp.js” zawiera

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

niż tylko trzeba zwrócić wartość w swoim dyrektywy AMD definicji modułu i to powinno działać:

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

tak że wystarczyły. –

Powiązane problemy