標籤:js angular
首先建立asp.NET mvc 程式,建立一個web api :
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 public class UserController : ApiController 2 { 3 private static List<User> userList = new List<User>() { 4 new User(){ UserID=1, Name="zhangsan"}, 5 new User(){UserID=2, Name="lisi"}, 6 new User (){UserID=3, Name="wangwu"}, 7 new User(){ UserID=4,Name="zhaoliu"} 8 }; 9 10 11 public IEnumerable<User> Get()12 {13 return userList.OrderBy(p => p.UserID);14 }15 16 public User Get(int id)17 {18 return userList.Where(p => p.UserID.Equals(id)).FirstOrDefault();19 }20 21 public void Post(User user)22 {23 userList.Add(user);24 }25 26 public void Put(User user)27 {28 userList.Remove(userList.Where(p => p.UserID.Equals(user.UserID)).FirstOrDefault());29 userList.Add(user);30 }31 32 }
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
其中User 類:
1 public class User2 {3 public int UserID { get; set; }4 public string Name { get; set; }5 }
這樣web api 就建立完畢了,接下來我們就要在項目中引用angular.js 所需要的js 檔案。可以在nuget 中下載直接搜尋anjular :
650) this.width=650;" src="http://images.cnitblog.com/blog/349270/201501/140116283701813.png" style="border:none;height:auto;margin:0px;padding:0px;" />
安裝完成之後,項目中就會在scripts檔案夾出現以下檔案:
650) this.width=650;" src="http://images.cnitblog.com/blog/349270/201501/140118429337169.png" style="border:none;height:auto;margin:0px;padding:0px;" />
然後在建立 一個AngularJS 檔案夾添加兩個js空檔案:app.js和controllers.js:
其中app.js 代碼:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 var userapp = angular.module("userapp", [‘ngRoute‘]);2 3 userapp.config([‘$routeProvider‘, function ($routeProvider) {4 5 $routeProvider.when(‘/Home/index‘, { templateUrl: ‘/Home/UserList‘, controller: ‘userlistController‘ })6 .when(‘/Home/EditUser/:id‘, { templateUrl: ‘/Home/EditUser‘, controller: ‘edituserController‘ })7 .otherwise({ templateUrl: ‘/Home/UserList‘, controller: ‘userlistController‘ });8 9 }]);
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
我們知道anjular.js 是模組化的所以定義了一個userApp模組, 在UserApp模組中,我們定義了路由和布局模板。userApp的預設URL是/Home/index,即http://localhost:10554/Home/Index。跳轉到編輯使用者的路由是/Home/EditUser/:id 其中:id 是傳遞的參數,如果不滿足這兩個路由的話就會跳轉到/Home/UserList 。我分別給每個路由指定了路由對應的地址以及controller。
controllers.js 代碼:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 userapp.controller(‘userlistController‘, function ($scope, $http) { 2 3 $scope.Users = []; 4 5 $scope.Load = function () { 6 $http.get("/api/user").success(function (data, status) { 7 8 $scope.Users = data; 9 })10 11 };12 20 $scope.Load();21 22 });23 24 25 userapp.controller(‘edituserController‘, function ($scope, $routeParams, $http) {26 27 $scope.UserID = $routeParams.id;28 $scope.User = {};29 $scope.Save = function () {30 $http.put("/api/user", { userid: $scope.User.UserID, name: $scope.User.Name }).success(function (data, status) {31 32 })33 };34 35 $http.get("/api/user/" + $scope.UserID).success(function (data, status) {36 $scope.User = data;37 })38 39 });
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
我定義了兩個controller :userlistController 和edituserController,這兩個controller 是在app.js中的路由中定義好的,必須一致。其中userlistController 是調用web api 來擷取使用者的列表,edituserController 是用來編輯使用者。$routeParams.id 是獲得傳遞過來的參數的。
這樣js 檔案基本寫好。然後看頁面是怎麼和angular.js 來配合使用的。
Home/Index.cshtml 代碼:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 <div ng-app="userapp"> 2 <div ng-view></div> 3 </div> 4 5 6 @section scripts{ 7 <script src="~/Scripts/angular.min.js"></script> 8 <script src="~/Scripts/angular-route.js"></script> 9 <script src="~/angularjs/app.js"></script>10 <script src="~/angularjs/controllers.js"></script>11 }
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
其中ng-app 是告訴頁面需要載入的是anjular.js 的那個模組,這裡是userapp。app.js 中已經定義。ng-view 是告訴angular.js 這一塊來呈現view 。其中angular-route.js檔案需要被引用,不然是不支援路由的。
然後我們需要在home 檔案夾建立UserList.cshtml 和EditUser.cshtml。
UserList.cshtml 代碼:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 @{ 2 Layout = null; 3 } 4 5 <div style="width:500px; margin:20px auto;"> 6 <table class="table table-bordered table-hover"> 7 <thead> 8 <tr> 9 ID10 </tr>11 <tr>12 UserName13 </tr>14 </thead>15 <tr ng-repeat="user in Users">16 <td>17 {{user.UserID}}18 </td>19 <td>20 {{user.Name}}21 </td>22 <td>23 <a class="btn-link" href="#/Home/EditUser/{{user.UserID}}">Edit</a>24 </td>25 </tr>26 </table>27 </div>
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
其中href="#/Home/EditUser/{{user.UserID}} 就是一個路由地址點擊的時候anjular.js 會檢查這個路由和那個路由相匹配然後跳轉到相應的url 中。
EditUser.cshtml 代碼:
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
1 @{2 Layout = null;3 }4 5 <div style="width:200px; margin:10px auto;">6 Name: <input ng-model="User.Name" />7 <br />8 <button class="btn btn-info" ng-click="Save()">Save</button>9 </div>
650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:none;margin:0px;padding:0px;" />
效果如下:
首先我們運行程式,一開始的時候的路由是/Home/Index 就會跳轉到/Home/UserList.cshtml 中
650) this.width=650;" src="http://images.cnitblog.com/blog/349270/201501/140142546203205.png" style="border:none;height:auto;margin:0px;padding:0px;" />
然後我們點擊編輯:注意查看地址欄的地址:
650) this.width=650;" src="http://images.cnitblog.com/blog/349270/201501/140144052455795.png" style="border:none;height:auto;margin:0px;padding:0px;" />
通過/Home/EditUser/1 這個路由,angular.js 就會讓頁面跳轉到/Home/EditUser.cshtml 這個頁面。點擊儲存成功。
650) this.width=650;" src="http://images.cnitblog.com/blog/349270/201501/140146105428716.png" style="border:none;height:auto;margin:0px;padding:0px;" />
angular.js的路由和模板在asp.net mvc 中的使用