gpt4 book ai didi

javascript - Asp.net mvc angularjs 奇怪的延迟

转载 作者:行者123 更新时间:2023-12-03 07:07:31 24 4
gpt4 key购买 nike

为什么当我第一次点击获取数据时没有任何反应,只有当我第二次点击按钮时,它才获取数据?为什么会发生这种延迟?

索引.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>

MyScripts.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');
});
};
}
});

家庭 Controller :

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 = function () {},您应该不会有任何问题。

关于javascript - Asp.net mvc angularjs 奇怪的延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36755318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com