我遇到了一个奇怪的问题。我在我的项目中使用AngularJS。我有一些部分视图,我有不同的AngularJS功能实现。我通过Ajax调用加载所有部分视图。 Ajax调用确实加载了容器中的局部视图,但其AngularJS功能不起作用。我注意到当我通过CDN引用AngularJS然后它可以工作,但是当我将CDN JS复制并粘贴到我的本地js文件中时,它就没有了。
请说明问题所在。
以下是代码:
部分视图:
<div ng-controller="EmployeeController">
<div>
ID: <input type="text" id="txtID" ng-model="employeeID" /><br />
Name: <input id="txtName" type="text" ng-model="employeeName" />
<button id="btnAddEmployee" ng-click="addEmployee()">Add Employee</button>
<button id="btnRemoveEmployee" ng-click="removeEmployee()">Remove Employee</button>
<ul >
<li ng-repeat="employee in employees">
Employee id is: {{employee.id}}<br />
Employee name is: {{employee.name}}
</li>
</ul>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
var employees = [{ name: 'ABC', id: '1' },
{ name: 'XYZ', id: '2' },
{ name: 'KKK', id: '3' }];
function EmployeeController($scope) {
$scope.employees = employees;
$scope.addEmployee = function () {
$scope.employees.push({ name: $scope.employeeName, id: $scope.employeeID });
}
$scope.removeEmployee = function () {
$scope.employees.pop();
}
}
</script>
控制器:
public PartialViewResult LoadViews(int id)
{
if (id == 1)
return PartialView("TestView1Partial");
else
return PartialView("TestView2Partial");
}
主要观点:
<ul>
<li>
<a href="#" onclick="LoadView2()">View 2</a>
</li>
</ul>
<div id="dvContainer">
<script>
function LoadView2() {
$.ajax({
url: "/home/LoadViews?id=2",
type: "GET",
datatype: "html",
async:true,
success: function (result) {
$("#dvContainer").html(result);
}
});
}
</script>
谢谢, JSHunjan
答案 0 :(得分:0)
在您的Ajax调用中更改async:false
,它可能会有效。