C# 使用angularjs获取数据不会';我不能正常工作
我正在尝试使用C# 使用angularjs获取数据不会';我不能正常工作,c#,angularjs,asp.net-mvc,asp.net-web-api,C#,Angularjs,Asp.net Mvc,Asp.net Web Api,我正在尝试使用angularjs开发一个简单的web应用程序,我有一个表city,它首先是使用entityframework数据库创建的 我将angularjs nuget添加到我的项目中,并在我的项目中创建了两个名为service和controller的文件,如下所示: 控制器 MyApp.controller('UpdateController', function ($scope, CityService) { getCities(); function getCiti
angularjs
开发一个简单的web应用程序,我有一个表city
,它首先是使用entityframework数据库创建的
我将angularjs nuget添加到我的项目中,并在我的项目中创建了两个名为service
和controller
的文件,如下所示:
控制器
MyApp.controller('UpdateController', function ($scope, CityService) {
getCities();
function getCities() {
CityService.getCities()
.success(function (cities) {
$scope.cities = cities;
})
.error(function (error) {
$scope.status = 'Unable to load customer data: ' + error.message;
});
}
});
服务:
MyApp.factory('CityService', ['$http', function ($http)
{
var urlBase = 'http://localhost:37404/api';
var CityService = {};
CityService.getCities = function () {
return $http.get(urlBase + '/default1');
};
return CityService;
}]);
我使用webapi传递数据,如您所见:
namespace MvcApplication8.Controllers
{
public class Default1Controller : ApiController
{
private testDBEntities db = new testDBEntities();
// GET: api/ManageStudentsInfoAPI
public IQueryable<City> GetStudent()
{
return db.Cities;
}
}
}
namespace mvcapapplication8.Controllers
{
公共类Default1控制器:ApiController
{
私有testDBEntities db=newtestdbentities();
//获取:api/ManageStudentsInfoAPI
公共IQueryable GetStudent()
{
返回db.城市;
}
}
}
以下是我的看法:
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title">
<a href="~/">ASP.NET Web API</a></p>
</div>
</div>
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/MyAngularFile/Service.js"></script>
<script src="~/MyAngularFile/Controller.js"></script>
</header>
<div id="body">
<div ng-controller="UpdateController">
<table class="table">
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr ng-repeat="a in cities">
<td>{{a.Id}}</td>
<td>{{a.Name}}</td>
<td>{{a.Country}}</td>
</tr>
</table>
</div>
</div>
身份证件
名称
国家
{{a.Id}}
{{a.Name}}
{{一个国家}
但结果是这样的:
我的浏览器错误:
试试这个:
var MyApp = angular.module('MyApp',[]);
然后,在html中:
<div id="body" ng-app="MyApp">
html代码中是否指定了ng app
?浏览器控制台中是否有任何错误(Ctrl+shift+i打开)?@Petter我的html代码中没有任何名为ng app的标记。我已将错误列表添加到我的postOk中,您需要一个ng app=“MyApp”
属性,例如在body标记中。此外,浏览器控制台还抱怨MyApp尚未定义。您可能需要添加var MyApp=angular.module('MyApp',[])代码>在Service.js文件中。您可以检查一个示例plunker:我收到此错误:错误:[$injector:modulerr]未能实例化模块MyApp,原因是:[$injector:nomod]模块“MyApp”不可用!您要么拼错了模块名,要么忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。我应该在哪里添加这一行var MyApp=angular.module('MyApp',[]);在控制器/服务实现之前,我可以添加var MyApp=angular.module('MyApp',[]);在另一个名为module的javascript文件中?尝试添加一个脚本app.js,其内容为var MyApp=angular.module('MyApp',[]);。然后,在Service.js和Controller.js中,将其添加到脚本顶部:var MyApp=angular.module('MyApp');