4

I work with angularjs and select options collection. If I added in html option tags all works good. But if i try added item from angular it is not working.

My code:

<div ng-controller="statisticsTableController">
  <select ng-model="publishersdata" 
          ng-options="s for s in publishersdata"
          chosen option="publishersdata">
  </select>
</div>

and AngularJs code:

function statisticsTableController($scope, $http) {
  var publishersArray = [];

  $http.get('/api/getAllItems').success(function(data) {  
    angular.forEach(data, function(data) {
      publishersArray.push(data.Name);
    });
  });

  this.publishersdata = publishersArray;
  //...
}

Why it is not working? And how to fix it?

UPDATE

After changes:

function statisticsTableController($scope, $http) {
    var publishersArray = [];

    $http.get('/api/getAllItems')
        .success(function (data) {
            angular.forEach(data, function (data) {
                publishersArray.push(data.Name);
            });
        });

    $scope.publishersdata = publishersArray;
    //...
}

HTML:

<div ng-controller="statisticsTableController">
  <select multiple 
          ng-model="publishersdata" 
          ng-options="s for s in publishersdata"
          chosen option="publishersdata">
  </select>
</div>

It is not working properly

5
  • push your data in $scope.publishersdata in your controller Commented May 15, 2015 at 7:20
  • One thing you shud be doing is change your ng-model name. Commented May 15, 2015 at 7:36
  • Sorry I don't understand. Can you explain me. Commented May 15, 2015 at 7:38
  • @TarasKovalenko your ng-model and ng-options array name are same, change one of them. this may cause problems. Commented May 15, 2015 at 7:42
  • Not working if I edit ng-model="publishersdata" to ng-model="publishers" . Why? Commented May 15, 2015 at 7:49

2 Answers 2

1

One problem that i see is that both your ng-model and your ng-options point to the same publishersdata object which might be one of the causes this brakes. Your ng-model should be an object which contains what is selected in the dropdown.

Also as posted above, you should attach publishersdata to $scope, otherwise it will not be accessible in the HTML

Other than that this looks fine to me.

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

Comments

0

try the code instead of data use value in argument of foreach

angular.forEach(data, function (value) {
           publishersArray.push(value.Name);
 });

9 Comments

what issue you have faced
what data you get from console i want it
I not get exception in browser console
I want your scuccess response output console.log(data)
|

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.