AngularJS:错误:没有控制器:形式 [英] AngularJS: Error: No controller: form
本文介绍了AngularJS:错误:没有控制器:形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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: 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
演示:小提琴
据我可以看到有在层次结构顶部的 NG-形式
元素,它应该提供从控制器到 gtInputMsg
指令。
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.
这怎么解决?
推荐答案
您正在编译&LT; gtux-文本框&GT;
没有它的形式下,元素DOM因为你没有追加它。尝试:
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);
在你的 gtuxEl
链接功能。
演示:小提琴
这篇关于AngularJS:错误:没有控制器:形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文