我在实现此angularjs教程时遇到了问题。当我更改模型值时,我的列表不会更新。
http://channel9.msdn.com/Events/Build/2014/3-644
这是我的代码。
<section class="section-container" id="HomeIndex" ng-app="userApp">
<article ng-controller="userListController">
<header>
<h1>Welcome to my site {{username}}</h1>
</header>
<form name="myForm">
<input name="username" ng-model="username" required/>
</form>
<ul>
<li ng-repeat="User in Users track by $index">{{User.Username}}</li>
</ul>
</article>
</section>
<script type="text/javascript">
var userService = angular.module("userService", ['ngResource']);
userService.factory('Users', ['$resource', function ($resource) {
return $resource('@this.Url.Action("FindUser","Home")', {}, {
query: { method: 'GET', params: { username: $resource.username }, isArray: true }
});
} ]);
var userController = angular.module("userController", []);
userController.controller('userListController', ['$scope', '$http', function ($scope, $http) {
$scope.Users = [];
$http({ method: 'GET', url: '@this.Url.Action("GetUser","Home")', params: { username: $scope.username} })
.success(function (data) {
$scope.Users = data;
});
} ]);
var userApp = angular.module('userApp', ['userController', 'userService']);
// userApp.filter('array', function () {
// return function (Users, username) {
// if (!username)
// return Users;
// var res = [];
// angular.forEach(Users, function (user) {
// if (user.Username.toLowerCase().indexOf(username) !== -1)
// res.push(user);
// });
// return res;
// };
// });
</script>
&#39; 控制器
public object FindUser(string username, int? max)
{
MyWeb.DataContext.UserDataContext mdc= new DataContext.UserDataContext();
JsonResult js = new JsonResult();
js.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
js.Data = mdc.M9s.Where(t => t.UserDomainName.Contains(string.IsNullOrWhiteSpace(username) ? "" : username)).Take(max != null || (max <= 100 && max > 0) ? Convert.ToInt32(max) : 100).Select(t => new { Username = t.UserDomainName.Trim(), JobID = t.JobID, JobFunction = t.JobFunction.Trim() }).ToList();
return js;
}
任何人都可以告诉我在更新输入模型时如何直接更新我的列表?