【发布时间】:2016-09-08 20:45:00
【问题描述】:
所以我有一个在许多不同情况下基本相同的表单,除了输入的名称可能会有所不同,并且表单所代表的数据会有所不同。
我从 this post 获得了可重用 sn-ps 的良好开端,但我想对此进行扩展,并弄清楚如何使其更具动态性。
HTML
<script type='text/ng-template' id="mySnippet">
<form>
{{mySinppet.firstThing}}<input id="first-input"/>
{{mySinppet.secondThing}}<input id="second-input"/>
</form>
</script>
<ng-include src="'mySnippet'" ng-model="thingOne"></ng-include>
<ng-include src="'mySnippet'" ng-model="thingTwo"></ng-include>
JS
$scope.thingOne={
firstThing: "1",
secondThing: "2",
foo: src["first-input"],
bar: src["second-input"],
};
$scope.thingTwo={
firstThing: "3",
secondThing: "4",
first: src["first-input"],
second: src["second-input"],
};
这是这段代码的代码(显然不起作用):https://plnkr.co/edit/iCOIq88e7gSSYaE92b0t?p=preview
那么如何设置 sn-p/ng-includes 以允许与模型通信?
【问题讨论】:
-
是时候考虑一个指令了。
-
您可以尝试 angular-formly (angular-formly.com/#) 来构建动态表单。您的 HTML 保持不变,仅更改 JSON 格式的表单数据
-
@Makoto 你能扩展一下吗?可能发布答案?
标签: angularjs