Archna Rangrej
Archna Rangrej

Reputation: 664

ng-init not working to initialize data

In AngularJS, ng-init is not working to initialize data before executing the template. For Example, index.html

<div class="control-group">
    <label class="control-label label-bold">Shipping First Name :</label>
    <div class="controls">
      <input class="control-width" validation-errors="contactData" 
         maxlength="50" type="text" id="shipping_first_name" 
         placeholder="First Name" 
         ng-model="contactData.data.ShopCustomer.shipping_first_name"
         ng-init="contactData.data.ShopCustomer.shipping_first_name='abc'">
    </div>
</div>

How do i get the initial value in AngularJS?

Here is the Fiddle : http://jsfiddle.net/aasquare3/2XDvt/

Upvotes: 1

Views: 11457

Answers (2)

Mark Rajcok
Mark Rajcok

Reputation: 364697

Working fiddle - abc shows in the textbox/input.

Your fiddle wasn't including Angular, so I added v1.0.3, and specified "No wrap - in <body>".

The body tag did not have ng-app, so I specified <body ng-app> under "Fiddle Options".

Upvotes: 1

Prathan Thananart
Prathan Thananart

Reputation: 4077

Are you sure contactData, contactData.data and contactData.data.ShopCustomer have been initialized (to empty objects at the very least) before attempting to set the value of contactData.data.ShopCustomer.shipping_first_name?

Upvotes: 0

Related Questions