[Angularjs]asp.net mvc+angularjs+web api单页应用详解
发布时间:2020-12-17 10:36:20 所属栏目:安全 来源:网络整理
导读:写在前面 项目结构 app = angular.module('app_store',['ngRoute','StoreService' span style="color: #008000;"// span style="color: #008000;"服务 span style="color: #0000ff;"var StoreService = angular.module('StoreService'span style="color: #00
写在前面
项目结构
app = angular.module('app_store',['ngRoute','StoreService'<span style="color: #008000;">//<span style="color: #008000;">服务
<span style="color: #0000ff;">var StoreService = angular.module('StoreService'<span style="color: #000000;">,[]); <span style="color: #008000;">//<span style="color: #008000;">请求服务 StoreService.factory('requestService',<span style="color: #0000ff;">function<span style="color: #000000;"> ($http,$q) { <span style="color: #0000ff;">var request =<span style="color: #000000;"> { method: 'POST'<span style="color: #000000;">,url: ''<span style="color: #000000;">,headers: { 'Content-Type': 'application/json'<span style="color: #000000;"> },data: {} }; <span style="color: #0000ff;">var postData =<span style="color: #000000;"> { lists: <span style="color: #0000ff;">function<span style="color: #000000;"> (type) { request.method = "get"<span style="color: #000000;">; request.url = "../api/order/lists/" + type + ""<span style="color: #000000;">; <span style="color: #0000ff;">return<span style="color: #000000;"> requestService($http,$q,request); },submit_product: <span style="color: #0000ff;">function<span style="color: #000000;"> (data) { request.method = "post"<span style="color: #000000;">; request.url = "../api/order"<span style="color: #000000;">; request.data =<span style="color: #000000;"> data; <span style="color: #0000ff;">return<span style="color: #000000;"> requestService($http,request); } }; <span style="color: #0000ff;">return<span style="color: #000000;"> postData; }); <span style="color: #0000ff;">function<span style="color: #000000;"> requestService($http,request) { <span style="color: #0000ff;">var deferred = $q.defer(); <span style="color: #008000;">//<span style="color: #008000;"> 声明延后执行,表示要去监控后面的执行 <span style="color: #000000;"> $http(request). success(<span style="color: #0000ff;">function<span style="color: #000000;"> (data,status,headers,config) { deferred.resolve(data); <span style="color: #008000;">//<span style="color: #008000;"> 声明执行成功,即http请求数据成功,可以返回数据了 <span style="color: #000000;"> }). error(<span style="color: #0000ff;">function<span style="color: #000000;"> (data,config) {
<span style="color: #000000;"> });
一个例子
app.controller('StoreController','StoreController' (!= "飞机" (data._code === 200=
app.config(['$routeProvider','/',{ templateUrl: '../Scripts/Views/OrderList.html',controller: 'StoreController''/error',{ templateUrl: '../Scripts/Views/Error.html',controller: 'ErrorController''/error'
<
{{item.Name}}
单价:{{item.Price}}
<span style="color: #0000ff;"></ <span style="color: #800000;">div<span style="color: #0000ff;">>
<span style="color: #0000ff;">namespace<span style="color: #000000;"> Wolfy.MvsSinglePage.Controllers
{ <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> StoreController : Controller { <span style="color: #008000;">//<span style="color: #008000;"> GET: Store <span style="color: #0000ff;">public<span style="color: #000000;"> ActionResult Index() { <span style="color: #0000ff;">return<span style="color: #000000;"> View(); } } }
<span style="color: #0000ff;"><<span style="color: #800000;">div <span style="color: #ff0000;">ng-view<span style="color: #0000ff;">></<span style="color: #800000;">div<span style="color: #0000ff;">>
<span style="color: #0000ff;">namespace<span style="color: #000000;"> Wolfy.MvsSinglePage.Controllers.api
{ <span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> OrderController : ApiController { <span style="color: #008000;">//<span style="color: #008000;"> GET: api/Order <span style="color: #000000;"> [HttpGet] [Route(<span style="color: #800000;">"<span style="color: #800000;">api/order/lists/{key?}<span style="color: #800000;">"<span style="color: #000000;">)] <span style="color: #0000ff;">public <span style="color: #0000ff;">async Task { <span style="color: #0000ff;">return <span style="color: #0000ff;">await Task.Run(() =><span style="color: #000000;"> { HttpResponseMessage response = <span style="color: #0000ff;">new<span style="color: #000000;"> HttpResponseMessage(HttpStatusCode.Accepted); List <span style="color: #0000ff;">new Order(){ Id=Guid.NewGuid(),Dt=DateTime.Now,Name=<span style="color: #800000;">"<span style="color: #800000;">飞机<span style="color: #800000;">",Price=<span style="color: #800080;">2222222<span style="color: #000000;">},<span style="color: #0000ff;">new Order(){ Id=Guid.NewGuid(),Name=<span style="color: #800000;">"<span style="color: #800000;">飞机2<span style="color: #800000;">",Name=<span style="color: #800000;">"<span style="color: #800000;">飞机3<span style="color: #800000;">",Name=<span style="color: #800000;">"<span style="color: #800000;">飞机4<span style="color: #800000;">",Name=<span style="color: #800000;">"<span style="color: #800000;">飞机5<span style="color: #800000;">",Name=<span style="color: #800000;">"<span style="color: #800000;">飞机6<span style="color: #800000;">",}; <span style="color: #0000ff;">var results = <span style="color: #0000ff;">string.IsNullOrEmpty(key) ? lst : lst.Where(x =><span style="color: #000000;"> x.Name.Contains(key)); response = <span style="color: #0000ff;">new<span style="color: #000000;"> HttpResponseMessage(HttpStatusCode.OK) { Content = <span style="color: #0000ff;">new StringContent(JsonConvert.SerializeObject(<span style="color: #0000ff;">new { _code = <span style="color: #800080;">200,_data =<span style="color: #000000;"> results })) }; <span style="color: #0000ff;">return<span style="color: #000000;"> response; }); } } }
总结
Demo Url:? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读