Javascript 如何让Angular JS ng仅在特定条件下初始化?
假设我有以下简短的输入表单:Javascript 如何让Angular JS ng仅在特定条件下初始化?,javascript,angularjs,angularjs-ng-init,Javascript,Angularjs,Angularjs Ng Init,假设我有以下简短的输入表单: <form ng-controller="parentController" action="testing.php" method="post"> <input name="parentInputField" ng-model="x"> <div ng-controller="childController"> <div ng-repeat="item in itemList">
<form ng-controller="parentController" action="testing.php" method="post">
<input name="parentInputField" ng-model="x">
<div ng-controller="childController">
<div ng-repeat="item in itemList">
<input name="childInputField[]" ng-model="x">
</div>
</div>
</form>
您可能已经知道,childController中的模型x将遵循parentController中模型x的值,直到用户在childController输入字段中键入某个内容,因此用户只需更改parent中的值以进行批量输入,然后在child中进行微调
现在,在用户提交表单后,我想调用相同的表单进行编辑。要将批量输入功能保留在新项目上,是否只有在存在以前的值时,我才能在childController中禁用init model x?尚未尝试,但我相信您可以通过以下方法实现:
<div ng-init="ctrl.value && (ctrl.value=1)">
但如果您需要建议,请避免像这样同时使用ng init和嵌套控制器:维护此程序将是一件痛苦的事情。更喜欢使用
controllerAs
syntax(),并将init逻辑放在控制器/服务上。您可能希望查看$pristine,并在满足条件时以编程方式进行设置。您的场景不是100%清楚,创建JSBin或其他东西可能会有所帮助。谢谢您的建议,但是如果我想保留批量输入函数,则有必要使用相同的模型名称来保留它们。如何启动具有相同模型名称的单个值?此外,您的解决方案似乎无法在没有旧数据的输入字段上保留批量编辑功能。