0

I have a problem with filling a scope with the correct data an keeping the correct attribute.

I've a small PHP script which returns an id, a name and a time as json array.

 {
  "times": [
    {
      "id": "myID",
      "name": "myName",
      "time": "40:00:00"
    },
    ...(10 more objects)
}

I want to create a dropdown element with the name as name and the id as value. I did this over a ng-repeat and this works very well.

<select name="StPl_Code" class="form-control" id="field-stpl-name" ng-model="selected">
    <option ng-repeat="i in times"  value="{{i.id}}">{{i.name}}</option>
</select>

My problem is, that I want to save the selected i in $scope.selected to use it's time value for an other input field without losing the value in the value attribute

Here is my angular code

  app.controller('auftraegeCrtl', function($scope, $http){
      $scope.selected = '';
      $http.get('index.php?page=times&json=true').success(function(data, status, headers, config){
        $scope.times = data.times;
      }).error(function(data, status, headers, config){
      });
  });
2
  • Use ng-change on select element...docs.angularjs.org/api/ng/directive/select Commented Nov 21, 2016 at 15:02
  • I thought there would be some working inline attributes instead of calling a function. I'll check this Commented Nov 21, 2016 at 15:04

3 Answers 3

1

You will want to use the ng-options directive, like this:

<select name="StPl_Code" class="form-control" id="field-stpl-name" ng-model="selected" ng-options="i.id as i.name for i in times"></select>

You can see it working here

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

1 Comment

I just found it <select name="StPl_Code" class="form-control" id="field-stpl-name" ng-model="selected" ng-options="item as item.name for item in times track by item.time">
0

Try this :

 $scope.selected = {};

instead of :

 $scope.selected = '';

Comments

0

Use 'Ng-options' instead.

<select ng-options="i as i.name for i in times track by item.id" ng-model="selected"></select>

This will set $scope.selected to the object selected

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.