0

I'm trying to create a directive 'pForm' so that I can easily create instances of forms inside of html pages via tags:

<p-form></p-form>

Each form will consist of the template: templates/form.html connected to an instance of the controller 'form' in the module 'forms'.

With that said, here's how I tried to approach it:

Main Module (in dependencies - forms module):

angular.module('mainModule', ['forms'])

.controller('MainController', ['$scope', function($scope) {

}]);

Forms Module:

angular.module("forms", [])

.controller("form", ['$scope', function($scope) {
    alert("init");
}])

.directive('pForm', function() {alert('directive');
    return {scope: {data: '=data'}, templateUrl: 'templates/form.html'};
});

The html file inits ng-app="mainModule", and I place in the html's body the following:

<p-form></p-form>

the alert 'directive' works, but the controller is not instantiated (no "init" alert).

I imagine I got the concept of directives somewhat backwards. Please let me know what you think.

1 Answer 1

1

change your directive to this:

.directive('pForm',function(){alert('directive');
return {
    scope:{
       data: '=data'
    },
   templateUrl:'templates/form.html',
   controller: 'form'
};
});

that will associate the controller to the directive

Sign up to request clarification or add additional context in comments.

4 Comments

Also, remove the alert('directive'). that may be causing an issue. If you want to verify it is being instantiated you can add this to the return statement: link:function(scope,element,attrs){alert('directive');}
On plunkr everything worked as expected. plnkr.co/edit/RTMEgZg20A1XhyjVBkgZ?p=preview I can ZIP the isolated problem for you, would you be able to go over it?
yep, go ahead and zip it up
Obviously it was something stupid, old angular version. Thanks for the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.