我正在尝试访问指令内的表单以进行验证,因此我想访问 $setPristine,但是,我似乎无法弄清楚如何获取使用 templateUrl 创建的表单。
我有一个 plunker 在这里详细说明了这个问题:http://plnkr.co/edit/Sp53xzdTbYxL6DAue1uV?p=preview http://plnkr.co/edit/Sp53xzdTbYxL6DAue1uV?p=preview
我收到错误:
Controller 'form', required by directive 'testDirective', can't be found!
这是相关的 Plunker 代码:
.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);
}
}
}
})
表单模板.html:
<form name="testForm" ng-click="save(testForm)">
<input type="text" ng-model="firstName" />
<br>
<input type="submit" value="Save" />
</form>
如何将 formTemplate.html 中的表单附加到指令的隔离范围?