AngularJS - 我如何访问我的指令中templateUrl内部定义的形式? [英] AngularJS - How do I access the form defined inside a templateUrl in my directive?
问题描述
我试图访问我的指令进行验证里面的形式,所以我想获得$ setPristine,但是,我似乎无法弄清楚如何获得的形式,如果它使用的是templateUrl创建的。
I am attempting to access the form inside my directive for validation purposes, so I'd like access to $setPristine, however, I can't seem to figure out how to get the form if it's created using a templateUrl.
我有一个plunker这里详细说明这个问题: http://plnkr.co/编辑/ Sp53xzdTbYxL6DAue1uV?p = preVIEW
I have a plunker detailing the issue here: http://plnkr.co/edit/Sp53xzdTbYxL6DAue1uV?p=preview
我得到一个错误:
Controller 'form', required by directive 'testDirective', can't be found!
下面是相关Plunker code:
Here is the relevant Plunker code:
.js文件:
var app = angular.module("myApp", []);
app.directive("testDirective", function() {
return {
restrict: 'E',
scope: {},
templateUrl: "formTemplate.html",
require: "^form", // <-- doesn't work
link: function (scope, element, attrs, ctrl) {
console.log(ctrl);
scope.open = function() {
// Would like to have access to the form here
// ctrl.$setPristine();
}
},
controller: function($scope) {
$scope.firstName = "Mark";
$scope.save = function(form) {
console.log(form);
}
}
}
})
formTemplate.html:
formTemplate.html:
<form name="testForm" ng-click="save(testForm)">
<input type="text" ng-model="firstName" />
<br>
<input type="submit" value="Save" />
</form>
我
如何能附上形式formTemplate.html我的指令的隔离范围?
How can I attach the form in formTemplate.html to the isolated scope of my directive?
推荐答案
的http:/ /plnkr.co/edit/41hhRPKoIsZ9C8Y9Yi87?p=$p$pview
试试这个在您的指令:
var form1 = element.find('form').eq(0);
formCtrl = form1.controller('form');
console.log(formCtrl);
这应该抓住窗体的控制器。
this should grab the controller for the form.
这篇关于AngularJS - 我如何访问我的指令中templateUrl内部定义的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!