2013-02-26 9 views
8

Ich bin nicht genau sicher, wie man eine Direktive anfordert und definiert, die ein requirjs Modul verwendet.Wie definieren wir eine Angularjs-Direktive in einem Requirejs-Modul?

dies ist mein Code für die Datei, die die Richtlinie Richtlinien enthalten/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'); 
      } 
     }; 
    }); 

}); 

Dies ist der Code meiner 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']); 
}); 

Antwort

12

Datei Sie sind in der Nähe. In Anbetracht, dass Ihre ‚Zf2NVIApp.js‘ Datei

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

enthält, als Sie nur den Wert in der Richtlinie AMD Moduldefinition zurückkehren müssen und es sollte funktionieren:

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

ja, dass der Trick. –

Verwandte Themen