【发布时间】:2016-04-14 23:57:08
【问题描述】:
我有一个现有的 Web 窗体 ASP.NET 网站。我想在调用我的 RESTful Web API 的网站上放置一个 AngularJS 页面。该页面有一个在页面加载时加载的证券选择列表。我按下一个按钮,调用另一个 RESTful 查询来显示所选证券的报价。当我直接在浏览器中调用 Web API 调用时,它们会起作用。 AngularJS 代码在到达“angularApp.controller('QuotesCtrl', function ($scope, $http)”行时失败。我认为这与应用程序/控制器/模块的嵌套方式有关。这是我第一次尝试 AngularJS,我想我已经很接近了,但我对此还不太了解。可能有一种简单的方法可以做到这一点。你能在下面的代码中看到错误吗?
<%@ Page Language="C#" MasterPageFile="~/Template.master" AutoEventWireup="true" CodeFile="WebAPI.aspx.cs" Inherits="WebAPIDemo" ValidateRequest="false" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
<script>
var angularApp = angular.module('myApp', []);
LoadSecurities();
function GetQuotes()
{
var id = $('#SecurityID').val();
id = id.replace("number:", "").trim();
var url= "http://stevegaines.info/api/Data/GetQuotes?id=" + id + "&count=10&extra=1";
angularApp.controller('QuotesCtrl', function ($scope, $http)
{
$http.get(url).
success(function (data)
{
$scope.Quotes = data;
});
});
return false;
}
function LoadSecurities()
{
angularApp.controller('SecuritiesCtrl', function ($scope, $http)
{
var url= "http://stevegaines.info/api/Data/GetSecurities?id=0&securityType=CASH";
$http.get(url).
success(function (data)
{
$scope.Securities = data;
});
});
return false;
}
</script>
<div ng-app="myApp">
<div ng-controller="SecuritiesCtrl">
<select ID="SecurityID" ng-model="SecuritiesModel" ng-options="x.SecurityID as x.TrueSymbol for x in Securities | filter:'USD'"/>
<button onclick="return GetQuotes();">Get Quotes</button><br />
<div id="Message"></div><hr />
</div>
<div ng-controller="QuotesCtrl">
<table id="QuotesTable">
<tr>
<th>Time</th>
<th>Open</th>
<th>Hi</th>
<th>Lo</th>
<th>Close</th>
</tr>
<tr ng-repeat="x in Quotes">
<td>{{ x.QuoteTime }}</td>
<td>{{ x.Open }}</td>
<td>{{ x.Hi }}</td>
<td>{{ x.Lo }}</td>
<td>{{ x.Close }}</td>
</tr>
</table>
</div>
</div>
</asp:Content>
【问题讨论】:
标签: javascript asp.net angularjs