Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

HTML:

<div ng-app="my-app" ng-controller="AppController">
    <ng-form name="myform">
        <gtux-el></gtux-el>
    </ng-form>
</div>

JS

var app = angular.module('my-app', [], function () {
})

app.controller('AppController', function ($scope) {
})

app.directive('gtInputMsg', ['$compile', '$interpolate', '$log', function($compile, $interpolate, $log) {
    function link($scope, element, attrs, ctrls) {
        var modelCtrl = ctrls[0], formCtrl = ctrls[1], msgCtrl = ctrls[2];

        element.on('click', function() {
            console.log('gt-input-msg:click', element)
        });
    };

    return {
        require : ['ngModel', '^form', 'gtInputMsg'],
        link : link
    };
}]);

app.directive('gtuxTextfield', ['$compile', '$timeout', '$log', function($compile, $timeout, $log) {
    return {
        restrict : 'E',
        template : '<input type="text" name="field" gt-input-msg ng-model="fieldvalue" />',
        replace : true
    };
}]);

app.directive('gtuxEl', ['$compile', '$timeout', '$log', function($compile, $timeout, $log) {
    function link($scope, $element, attrs, ctrl) {
        //here compile is used because in my use case different elements are used based on some option values. A service provides the template to be used based on a key
        var $cr = $compile('<gtux-textfield></gtux-textfield>')($scope);
        $($cr).appendTo($element);
    }

    return {
        restrict : 'E',
        link : link,
        transclude : true
    };
}]);

Error

Error: No controller: form
    at Error (<anonymous>)
    at getControllers (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:4278:19)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js:4284:24

Demo: Fiddle

As far I can see there is a ng-form element at the top of the hierarchy which should provide the from controller to the gtInputMsg directive.

How can this be rectified?

share|improve this question
1  
The problem is that you don't have a form element on the page. ng-form is meant to be used implicitly. –  finishingmove Aug 21 '13 at 18:00
add comment

1 Answer

up vote 2 down vote accepted

You're compiling the <gtux-textfield> element without it being under the form in the DOM as you haven't appended it yet. Try:

var $cr = $('<gtux-textfield></gtux-textfield>').appendTo($element);
$compile($cr)($scope);

In your gtuxEl link function.

Demo: Fiddle

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.