简体   繁体   中英

Using ng-options in AngularJS to bind data in a <select> tag

I want to bind all categories in a using ng-options. I used multiple controllers in one html page. ie, product.html. When i run my code,getting empty list

product.html

<html data-ng-app="crm">
<body data-ng-controller="ProductController">
  .............
  <form class="form-horizontal">      
  <div class="col-md-4 col-sm-4 col-xs-4 left">
    <div class="input-group" data-ng-controller="CategoryController">
      <select class="form-control" data-ng-model="category.nameCategory" data-ng-options="cat.nameCategory for cat in categories track by cat.idCategory"></select>
    </div>
  </div>
  </form>
  ................
 </body>
 </html>

product.js

var app=angular.module('crm');
    app.controller('ProductController', function($scope,$http,$document){
    ........
    ........
    });

    app.controller('CategoryController', function($scope,$http){
        $http.get("/allCat")
        .then(function(response){
            $scope.categories=response.data;
        });
});

No need of using 2 controllers.

I think this will work for you. If not please show response data.

app.controller('CategoryController', function($scope, $http) {
  $http.get("/allCat")
    .then(function(response) {
      $scope.categories = response.data.categories;          
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM