我有一個現有的Web窗體ASP.NET網站。我想在調用我的RESTful Web API的網站上放置一個AngularJS頁面。該頁面有一個在頁面加載時加載的證券選擇列表。我按下一個按鈕,調用另一個RESTful查詢來顯示所選安全性的引號。當我在瀏覽器中直接調用它們時,Web API調用工作。當AngularJS代碼到達讀取「angularApp.controller('QuotesCtrl',函數($ scope,$ http)」的行時失敗。我認爲這與應用程序/控制器/模塊的嵌套方式有關。是我在AngularJS上的第一次嘗試,我認爲我很接近,但我對它還不是很瞭解,可能有一個簡單的方法可以做到這一點,你可以在下面的代碼中看到這個錯誤:如何在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>
感謝。這有幫助。我會看看你發佈的學習Angular鏈接。也許現在我知道足夠去上學了。 :-) –
很高興幫助,當我開始有角度的時候,花了我一個月左右的時間就放棄了jquery!從未回頭! – garethb