Asp.net MVC Angularjs 奇怪的延迟
Asp.net mvc angularjs strange delay
为什么当我第一次点击Get Data
没有任何反应,只有当我第二次点击按钮时,它才会获取数据?为什么会出现这种延迟?
Index.cshtml:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<script src="~/Scripts/CustomScripts/MyScript.js"></script>
<script src="~/Scripts/moment.js"></script>
<link rel="stylesheet/less" type="text/css" href="~/Scripts/CustomScripts/style.less">
<script src="~/Scripts/less-1.5.1.js" type="text/javascript"></script>
<li ng-repeat="employee in Employees">
{{employee.name}}
</li>
<button ng-click="getData()">Get Data</button>
我的脚本.js
var app = angular.module("myApp", []);
app.controller("calendarDemo", function ($scope, $http) {
$scope.id = '5';
$scope.Employees = [];
$scope.getData = function () {
$scope.getData = function (id) {
$http({ method: 'GET', url: '/Home/GetData/', params: { id: $scope.id} }).
success(function (data, status, headers, config) {
$.each(data, function (id, data) {
$scope.Employees.push({name: data.Name});
})
debugger;
}).
error(function (data, status, headers, config) {
alert('error');
});
};
}
});
主控制器:
namespace AngularJS.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult GetData(int id=0)
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { Name = "Jhon" });
employees.Add(new Employee { Name = "Rick" });
employees.Add(new Employee { Name = "Tony" });
return Json(employees, JsonRequestBehavior.AllowGet);
}
}
public class Employee
{
public string Name { get; set; }
}
}
_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/angularjs")
</head>
<body ng-app="myApp" ng-controller="calendarDemo">
@RenderBody()
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
</body>
</html>
该函数被定义两次,一个嵌套在另一个内部。删除外部 $scope.getData = 函数 () {},您应该没有问题。
相关文章:
- 延迟 AngularJS 路由更改,直到加载模型以防止闪烁
- Asp.net MVC Angularjs 奇怪的延迟
- 如何使用angularjs延迟加载谷歌jsapi图表
- Angularjs模型更新延迟
- AngularJS正在等待CSS延迟加载
- 如何使用AngularJS在延迟后启用按钮
- AngularJS搜索延迟,直到用户输入字段
- angularjs ng-include中html文件的延迟包含
- Javascript 或 angularjs 在刷新之间延迟浏览器关闭或选项卡关闭
- 如何处理 AngularJS 中 $http.post 的延迟
- MouseEnter 事件失火,即使在 AngularJS 中也有延迟
- AngularJS:承诺链不延迟超过第二个函数
- AngularJS延迟承诺不推迟
- AngularJS 1.2.0:延迟加载控制器和模板
- AngularJS中ngRepeat集合中的延迟加载项
- 带有 angularjs 和 requirejs 的延迟加载模块
- 弹出框延迟显示在鼠标输入(Angularjs)上
- 为什么 AngularJS 不能与多个延迟脚本一起工作
- 如何在对 AngularJS 控制器进行单元测试时模拟网络延迟
- 需要帮助来延迟angularjs中的搜索结果