【问题标题】:How to populate dropdownlist using WCF Rest AngularJs如何使用 WCF Rest AngularJs 填充下拉列表
【发布时间】:2017-04-29 11:10:45
【问题描述】:

如何使用 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,我这样做是行不通的。请帮忙。谢谢

【问题讨论】:

  • 您忘记在控制器中注入服务...您是否遇到任何特定错误?
  • 没有任何错误。我知道这缺少注入控制器。但我不知道该怎么做
  • 当我在 entryCtrl.js 中添加$scope.Category(); 时出现错误,例如GetCategoryService is not defined at ChildScope.$scope.Category
  • 这就是我所说的错误...

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


【解决方案1】:

确保您已将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>

【讨论】:

  • @Stfvns 我不明白
  • 使用类似entryCtrl.js的结构。当我使用喜欢你的答案时,它不是从我的 wcf 休息服务@Sajeetharan 获得的
  • 就这样改,
  • 是的。然后我编辑我的entryCtrl.js entryCtrl.$inject = ['$scope', 'GetCategoryService']; 然后就可以了
  • 没有帮助。你不回答添加entryCtrl.$inject = ['$scope', 'GetCategoryService'];这是重要的思考我的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-11
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
相关资源
最近更新 更多