0

How to set value and get the value in the form of $scope.object1.object = "some data";

In html and js if i set data like this it will work.

<select ng-model="myColor" ng-options="color.name for color in colors">

 $scope.colors = [
          {name:'black', shade:'dark'},
          {name:'white', shade:'light'},
          {name:'red', shade:'dark'},
          {name:'blue', shade:'dark'},
          {name:'yellow', shade:'light'}
        ];

But actually i want like these

 <select ng-model="myColor" ng-options="color.name for color in test.colors">

  $scope.test.colors = [
          {name:'black', shade:'dark'},
          {name:'white', shade:'light'},
          {name:'red', shade:'dark'},
          {name:'blue', shade:'dark'},
          {name:'yellow', shade:'light'}
        ];

See the Example in plunker demo

6
  • 1
    Please post code within question, not just in external link. Also question needs a bit more effort to be clear what your issues are. WHat does jsp have to do with anything here? Commented Nov 19, 2014 at 13:14
  • what kind of issues are you getting? test is undefined? Commented Nov 19, 2014 at 13:16
  • I am not getting any error and also i updated plunk url. now what i want exactly is while setting data i want to set like $scope.test.colors = data; and in HTML i want to access like test.colors Commented Nov 19, 2014 at 13:20
  • click on this plnkr.co/edit/g5mGdHP5LHY8Y1xiP9rm?p=preview url. here i am not able to see the data in dropdown. while setting $scope.test.colors it is causing. if you set $scope.colors it will work. Commented Nov 19, 2014 at 13:24
  • 2
    the error if because your test is not define.. you need to do something like: $scope.test = {}; $scope.test.color = []; now should work. Commented Nov 19, 2014 at 13:38

1 Answer 1

3

See this plunkr

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    // ADD THIS NEXT LINE
    $scope.test = {}
    $scope.test.colors = [
      {name:'black', shade:'dark'},
      {name:'white', shade:'light'},
      {name:'red', shade:'dark'},
      {name:'blue', shade:'dark'},
      {name:'yellow', shade:'light'}
    ];
    $scope.myColor = $scope.test.colors[2]; // red
  }]);
Sign up to request clarification or add additional context in comments.

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.