1

I am stuck AngularJS and JSON. I have view called form.html that allow user can select their province. I created Province JSON file and put in select tag, but when I store mySQL I need a province Id I tried ng-value="province.id" in option tag but doesn't work. How can I get a pid(provinceId) at same time when user select province?

province.JSON

[
    {
    "pid" : 1,
    "name" : "Ontario"
    },
    {
    "pid" : 2,
    "name" : "Québec"
    },
    {
    "pid" : 3,
    "name" : "Nova Scotia"
    },
    {
    "pid" : 4,
    "name" : "New Brunswick"
    },
     ...

form.html

<select name="province" ng-model="user.province" ng-required="true"> 
 <option value="">--- Please select ---</option> 
 <option ng-repeat="province in provinces">{{province.name}}</option>
</select>

controllers.js

$scope.submitForm = function (user) {              
    dataObj = {
        "name": user.name,           //it works and all fine
        "province": user.province,   //it works and all fine
        "telephone": user.telephone, //it works and all fine
        "postalcode": user.postalcode, //it works and all fine
        "salary": user.salary,         //it works and all fine
        "provinceId" : user.pid       // undefine .. 
    }
8
  • And the PHP part is...? Commented Sep 26, 2017 at 6:46
  • <option ng-repeat="province in provinces" ng-value="province.pid">{{province.name}}</option> ? Commented Sep 26, 2017 at 6:48
  • Does PHP matter? I think my php is okay Commented Sep 26, 2017 at 6:49
  • I tried but it doesn't work AJ Commented Sep 26, 2017 at 6:50
  • Try using ng-options instead ng-repeat. Commented Sep 26, 2017 at 6:53

2 Answers 2

1

Try this:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.user = {};
    $scope.provinces = [{
                        "pid" : 1,
                        "name" : "Ontario"
                        },
                        {
                        "pid" : 2,
                        "name" : "Québec"
                        },
                        {
                        "pid" : 3,
                        "name" : "Nova Scotia"
                        },
                        {
                        "pid" : 4,
                        "name" : "New Brunswick"
                        }
                      ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
 <select name="contactCountry" ng-model="user.province" ng-options="item.pid as item.name for item in provinces" ><option value="">Select provinces</option>
 </select> 
 <br>
 <p>Selected Province Id = {{user.province}}</p>
</div>

Hope it helps!!.

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

2 Comments

thanks! it works but I need province.name as well to just display how can I get a province name at the same time?
thanks, It actually worked for me with editing some code I got a concept from you thanks!
0

when user submit select tag will be as object like {"pid:1", "name":"Ontario"} so I can fetch in my controller

The point is I should make as oject.

html file

 <select name="province" ng-model="user.province" ng-options="item.name for item in provinces" class="form-control input-lg" ng-required="true"> 
    <option value="">Select provinces</option>
 <select>

controllers.js

    myFactory.set(user);                 
    dataObj = {
        "name": user.name, 
        "province": user.province.name,   
        "telephone": user.telephone,
        "postalcode": user.postalcode, 
        "salary": user.salary,
        "pid" : user.province.pid
    }

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.