1

I'm trying to populate the form with Data that are coming from Database/JSON.

I can populate the text field but for some reason the select drop down and the checkbox are not being populated with data. The select should have "male" and the checkbox should be ticked because it's set true.

code:

    <form>
    <label>Gender:</label>
    <select id="s1" ng-model="myData.gender"></select>
    <br>
    <label>Name:</label>
    <input type="text" ng-model="myData.name">
    <br>
    <label>Active:</label>
    <input type="checkbox" ng-model="myData.active" value="" id="checkbox" name="check" ng-init="myData.active=myData.active"><br>
    <input type="button" value="Save" ng-click="Update">
  </form>

    //assume these data coming from database
      $scope.myData = {
        "name" :  "John",
        "active" : "true",
        "gender" : "male"
      };

So basically the whole idea is that, when the fields are populated, the user can change the values and then hit the Save button which will update the database with new data.

Can anyone help?

PLUNR

4 Answers 4

2

You need to make some changes in template

<label>Gender:</label>
<select 
  id="s1" 
  ng-model="myData.gender" 
  ng-options="sex for sex in ['male', 'female']">
</select>

<br>
<label>Name:</label>
<input 
  type="text" 
  ng-model="myData.name">

<br>
<label>Active:</label>
<input 
 type="checkbox"  
 ng-model="myData.active"  
 id="checkbox" 
 name="check" >

<br>
<input type="button" value="Save" ng-click="Update">

and in data

//assume these datra coming from database
$scope.myData = {
    "name" :  "John",
    "active" : true,  // it will be true not string "true"
    "gender" : "female"
  };

here is working pluck

If data from db is not valid Boolean use Transformer to make data valid for js. in short cast the int, and boolean from db.

Sign up to request clarification or add additional context in comments.

Comments

1

For starters, your true value is a string and not a boolean (use true instead of "true"), so that's why the checkbox is not working.

$scope.myData = { "name" : "John", "active" : "true", "gender" : "male" };

$scope.myData = { "name" : "John", "active" : true, "gender" : "male" };

Secondly your select does not have any options, so you should add the following to your HTML.

<option value="male">Male</option>
<option value="female">Female</option>

Or use ng-options.

Plunkr

Comments

1

First of all you should go for a good tutorial :-

You cannot bind select directly with ng-model it's not valid.

Now instead of simple myData as a object you need to create a array because ng-options can only work with array.

$scope.myData = [{
    "name" :  "John",
    "active" : "true",
    "gender" : "male"
  }];

In HTML:

 <select id="s1" ng-model="selected" ng-options="item.gender for item in myData">
    </select>

Here is plunker

Comments

1

Also you can add the 'ng-checked' directive to the input checkbox (see this)

<input type="checkbox" ng-checked="myData.active" ng-model="myData.active" value="" id="checkbox" name="check" ng-init="myData.active=myData.active">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.