AngularJS与MVC局部视图工作 [英] AngularJS not working with MVC Partial Views
问题描述
我有一个奇怪的问题。我用我的项目AngularJS。我有我在哪里具有AngularJS功能的不同实现一些局部视图。我加载通过Ajax调用我所有的部分景色。 Ajax调用加载并局部视图容器,但其AngularJS功能不起作用。我注意到,当我给参考通过CDN AngularJS然后它工作,但是当我复制和粘贴CDN JS到我的本地的js文件,那么它没有。
I am having a strange problem. I am using AngularJS in my project. I have some partial views where I am having different implementations of AngularJS functionality. I am loading all my partial views via Ajax call. Ajax call does load partial view in container but its AngularJS functionality does not work. I have noticed that when I give reference to AngularJS via CDN then it works but when I copy and paste CDN JS into my local js file then it does not.
请说明是什么问题。
下面是code:
局部视图:
<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
Thanks, JSHunjan
推荐答案
在你的Ajax调用修改异步:假
键,它可能工作
In your Ajax call change the async:false
and it might work.
这篇关于AngularJS与MVC局部视图工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!