gpt4 book ai didi

javascript - AngularJS如何在与指令相同的级别要求ngForm

转载 作者:行者123 更新时间:2023-11-30 17:13:20 25 4
gpt4 key购买 nike

我试图在我的指令中要求与 ngRepeat 的每次迭代关联的 ngForm。我试过使用 require: 'form' 但它返回时未定义,也尝试过 '^form',但它在 ngRepeat 之外给出了 mainForm。有没有像 require: 'ngForm' 这样的特殊方法,它会在我的表单 View 指令的第一个元素上为我提供 ngForm 的 Controller ?

表单 View 指令之外的父指令模板

... // some other markup
<form name="mainForm">
... // some other markup

// Vertically stacked tabs directive
// Creates left/right cols, left=stacked tabs, right=form associated content using transclude
<pills>

// Pill pane content with title attribute passed up to pills during compile for stacked tabs
<pill ng-repeat="form in formData.documents"
title="{{form.name}}">

// Generated form content for each pill pane
<form-view form="form"
model="formModel"></form-view>

</pill>

</pills>

... // some other markup
</form>

表单 View 指令模板

<fieldset ng-form="form.id"> // want reference to this controller using require

<h3 ng-bind="form.name"></h3>
<div ng-repeat="field in form.mapping.fields"

... // bunch of generated fields
</div>

</fieldset>

表单 View 指令

.directive('formView', [function() {

return {
restrict: 'E',
templateUrl: '/app/components/form-view/formview.html',
scope: {
form: '=',
model: '='
},
require: ['^tabs', '^pills', '^form'], // also tried 'form', '?form', 'ngForm', 'ngform'
link: function( $scope, $element, $attrs, ctrls ) {
$scope.tabsCtrl = ctrls[0]; // got tabs
$scope.pillsCtrl = ctrls[1]; // got pills
$scope.formCtrl = ctrls[2]; // always undefined or grabs mainForm
}
};
}]);

最佳答案

你不能“要求”指令元素内的东西,只能在它上面或上面。

实现此目的的一种方法是制作您自己的 ngForm 指令,该指令需要您的 formView,然后与它对话。它将沿用现有的 ngForm。

查看 ngForm 和 ngModel 的实现以获得真实示例 - ngForm 知道其中的 ngModels 但这不是因为表单找到模型,模型让表单知道它们的存在。

关于javascript - AngularJS如何在与指令相同的级别要求ngForm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com