1

Let say I have the following short input form:

<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>

As you may already know, the model x in childController will follow the value of of model x in parentController until the user type in something in the childController input field, so the user can simply change the value in parent for bulk input and then fine tune in child.

Now, after the user have submitted the form, I want to call the same form for editing function. To keep the bulk input function on new items, is there a way I can ng-init model x in childController only when there is a previous value?

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
cytsunny
  • 4,838
  • 15
  • 62
  • 129
  • You might want to look into $pristine and setting that programmatically when your conditions are met. You scenario isn't 100% clear, it may help to create a JSBin or something. – Scott Sword Apr 17 '15 at 03:35

1 Answers1

1

Haven't tried but I believe you can achieve with:

<div ng-init="ctrl.value && (ctrl.value=1)">

BUT if you want an advice, avoid both ng-init and nesting controllers like this: it would be a pain to maintain this program. Prefer to use controllerAs syntax (https://docs.angularjs.org/api/ng/directive/ngController) and put init logic on controller/service.

André Werlang
  • 5,839
  • 1
  • 35
  • 49
  • Thanks for your advice, but if I want to keep that bulk input function, it is necessary to keep them using the same model name. How to initiate an individual value with the same model name? – cytsunny Apr 17 '15 at 03:36
  • Also, it seems that your solution cannot keep the bulk-edit function on input fields with no old data. – cytsunny Apr 17 '15 at 03:38