我一直在努力寻找例子,但根本找不到任何东西。我唯一知道的是我可以使用http模块来获取数据。这是我当前正在执行的操作,但它是使用Knockout编码的。有人可以给我一些有关如何使用AngularJS重新编码此功能的建议吗?
的HTML
<select id="testAccounts" data-bind="options: testAccounts, optionsValue: 'Id', optionsText: 'Name', optionsCaption: 'Select Account', value: selectedTestAccount"> </select>
Java脚本
<script type='text/javascript'> $(document).ready(function () { var townSelect = function () { var self = this; self.selectedTestAccount = ko.observable(); self.testAccounts = ko.observableArray(); var townViewModel = new townSelect(); ko.applyBindings(townViewModel); $.ajax({ url: '/Admin/GetTestAccounts', data: { applicationId: 3 }, type: 'GET', success: function (data) { townViewModel.testAccounts(data); } }); }); </script>
正确的方法是使用ng- options指令。HTML看起来像这样。
ng- options
<select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts"> <option value="">Select Account</option> </select>
JavaScript:
angular.module('test', []).controller('DemoCtrl', function ($scope, $http) { $scope.selectedTestAccount = null; $scope.testAccounts = []; $http({ method: 'GET', url: '/Admin/GetTestAccounts', data: { applicationId: 3 } }).success(function (result) { $scope.testAccounts = result; }); });
您还需要确保Angular在html上运行并且模块已加载。
<html ng-app="test"> <body ng-controller="DemoCtrl"> .... </body> </html>