[英]Angular JS: How to load angular js partial view page in specific element after button click
Angular js 部分視圖頁面在按鈕單擊后未加載。 一旦我們單擊按鈕,我已經加載了初始渲染頁面。 部分視圖頁面將呈現,此時 angular 模塊不會被觸發並且不會返回正確的文本。 我怎樣才能實現這種類型的場景?
布局.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - Application</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.8/angular.min.js"></script>
<script src="~/Scripts/app.js"></script>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<div id="content"><button onclick="clicked()">Click Here</button></div>
</div>
@RenderBody()
<script>
function clicked() {
$.ajax({
type: "GET",
url: "/Home/About",
data: null,
dataType: "html",
contentType: 'application/json; charset=utf-8',
success: function (data) {
$('#content').append(data);
},
error: function (ex) {
console.log(ex);
}
});
}
</script>
</body>
</html>
應用程序.js
var myApp = angular.module('myApp', []);
myApp.controller('MyController', ['$scope', function ($scope) {
// Function to be called on page load
$scope.firstFunction = function ($scope) {
$scope.text = "GeeksForGeeks"
}
}]);
家庭控制器.cs
public PartialViewResult About()
{
ViewBag.Message = "Your application description page.";
return PartialView(@"~/Views/Home/About.cshtml");
}
部分視圖頁面: About.cshtml
<div ng-controller="MyController">
<center ng-init="firstFunction(this)">
<h1 style="color: green;">{{text}}</h1>
</center>
</div>
注意:點擊按鈕時, GeeksForGeeks需要顯示,這里{{text}}附加在 UI 元素中
嘗試從您那里刪除錯誤的數據和內容類型 ajax
$.ajax({
type: "GET",
url: "/Home/About",
success: function (data) {
$('#content').append(data); //or try $('#content').html(data);
},
error: function (ex) {
console.log(ex);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.