0

I have a list of objects with 2 attributes: label and description. On html I cycle on this list:

<select class="form-control" 
     ng-options="fid.label for fid in myFidList" 
     ng-model="fidSelected" 
     ng-change="setFidDescription()"/>

What I'm trying to do is calling a function (setFidDescription) to get the object I have selected on option tag. So I can change the respective desciption in another div. That's seems not working: ng-model seems not updating.The function:

 $scope.setFidDescription = function () {
    console.log($scope.fidSelected);
 }

This code print an empty object. What am i missing? Another way to go could be get the $index of my choice.

More info: JSON + console

Update: I have found my problem. See here for the explanation:

AngularJS ngModel doesn't work inside a ui-bootstrap tabset

That was my problem.

3
  • post your json data Commented Nov 4, 2016 at 6:15
  • Are you getting console right data at : console.log($scope.fidSelected); ? Commented Nov 4, 2016 at 6:21
  • no, I print an object without attributes Commented Nov 4, 2016 at 13:29

1 Answer 1

1

// Code goes here

angular
  .module('myApp', [])

.controller('testController', ['$scope',
  function($scope) {

    $scope.myFidList = [{
      label: 'label1',
    }, {
      label: 'label2',
    }]

    $scope.setFidDescription = function() {
      alert($scope.fidSelected.label);
    }

  }
])
<!DOCTYPE html>
<html data-ng-app="myApp">

<head>
  <link rel="stylesheet" href="style.css">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
  <script src="script.js"></script>
</head>

<body data-ng-controller="testController">



  <select class="form-control" ng-options="fid.label for fid in myFidList" ng-model="fidSelected" ng-change="setFidDescription()"></select>

</body>

</html>

Hope this will help you.

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

3 Comments

thansk for the reply. But I keep getting "undefined". The fidSelected is empty.
@Massimo, in my example also?
Yes, with your code: example. Note: the ng-repeat is working fine

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.