Search code examples
javascriptangularjsangularjs-scope

Issue with ng-init scope variable on ng-submit


I have the following input field with ng-init scope variable

<input type="hidden" ng-model="formData.TEST" ng-init="formdata.TEST='{{ scopeVariable }}'" value="{{ scopeVariable }}" />

The {{ scopeVariable }} value get from controller:

$scope.scopeVariable = '123456';

When check the field value from the developer tools this looks like this:

<input type="hidden" ng-model="formData.TEST" ng-init="formdata.TEST='123456'" value="123456" />

But when submit the form with ng-submit I got the following object:

LOREM: 'IPSUM'
IPSUM: 'LOREM'
TEST: "{{ scopeVariable }}"

How can I init the model value correctly?


Solution

  • ng-init is already an expression, so you don't need to add the handlebars.

    ng-init="formdata.TEST = scopeVariable" should be sufficient.

    Also be careful of casing. formData and formdata are two different objects! Lastly the value attribute is unnecessary as you have ng-model. So I would write it as:

    <input type="hidden" ng-model="formData.TEST" ng-init="formData.TEST = scopeVariable" />