如何使用WCF Rest AngularJs填充下拉列表

时间:2016-12-14 07:42:36

标签: c# asp.net angularjs wcf-rest

如何使用WCF Rest Service填充下拉列表:

首先,这是我获得服务的代码:

service.js

(function (app) {
app.service("GetCategoryService", function ($http) {
    this.GetCategory = function () {
        return $http.get("http://localhost:51458/ServiceRequest.svc/GetCategory/");
    };
});
})(angular.module('entry'));

Entry.Ctrl

(function (app) {
    'use strict';
    app.controller('entryCtrl', entryCtrl);
    entryCtrl.$inject = ['$scope'];

    function entryCtrl($scope) {
        $scope.pageClass = 'page-entry';
        //To Get Category
        $scope.Category = function () {
            var promiseGet = GetCategoryService.GetCategory();
            promiseGet.then(function (pl) { $scope.GetCategory = pl.data },
                  function (errorPl) {
                      console.log('Some Error in Getting Records.', errorPl);
                  });
        }
    }
})(angular.module('entry'));

这是entry.html

<div class="dropdown">
    <select ng-model="Category" ng-options="item.ITEM_TEXT for item in Category"></select>
</div>

WCF输出JSON如: [{"ITEM_TEXT":"INTERNAL APP"},{"ITEM_TEXT":"INTERNAL IT"}]

我不知道如何将此传递给html,我正在做的事情就是这样。请帮忙。感谢

1 个答案:

答案 0 :(得分:2)

确保您已将ng-model设置为与array name不同,并将服务注入您的控制器,

entryCtrl.$inject = ['$scope', 'GetCategoryService'];

<强> DEMO

var app = angular.module('todoApp', []);

app.controller("dobController", ["$scope",
  function($scope) {
 
    $scope.Category = [{"ITEM_TEXT":"INTERNAL APP"},{"ITEM_TEXT":"INTERNAL IT"}];
  }
]);
<!DOCTYPE html>
<html ng-app="todoApp">

<head>
  <title>To Do List</title>
  <link href="skeleton.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
  <script src="MainViewController.js"></script>
</head>


<body ng-controller="dobController">
   <select ng-model="selected"  ng-init="selected = Category[0]" ng-options="item.ITEM_TEXT for item in Category"></select>
</body>

</html>

相关问题