舉例來說,在Views/Index.cshtml
下,我有一個輸入表單,只要輸入一次,一旦填寫並提交,就會經過控制器js文件中的一個函數,然後window.location.pathname
重定向到另一個頁面。該下一頁與另一個控制器/ js文件相關聯。如何在ASP MVC設置中將AngularJS中的變量從一個控制器傳遞到下一個?
如何將一對變量從一個傳遞給另一個?
我試着做這種方式,它不工作:
一次嘗試
_Layout.cshtml
:
<html ng-app="MIScheduler">
<head>
...
</head>
<body>
@RenderBody()
// AngularJS scripts
<script src="app.js"></script>
</body>
</html>
Index.cshtml
:
<input ng-model="userID" name="userID" />
<input ng-model="userEmail" name="userEmail" />
<input type="submit" id="submit" />
@section scripts {
<script src="HomeController.js"></script>
}
HomeController.js
:
var CID = null;
var Email = null;
$scope.submit = function() {
CID = $scope.userID;
Email = $scope.userEmail;
window.location.pathname = 'Home/OtherPage';
};
然後,當重定向到OtherPage.cshtml
:
{{cid}}
{{email}}
@section scripts {
<script src="OtherPageController.js"></script>
}
不輸出任何東西,如果我OtherPageController.js
:
$scope.cid = CID;
$scope.email = Email;
我也試過在app.js
實現服務和傳遞對兩個控制器的服務,但沒有輸出這種方式。
第二次嘗試
這裏是我的第二個方法我試過:app.js
:
angular.module('moveinScheduler', [])
.service('ShareUserInfo', function() {
LData = {
cid: '',
email: ''
}
return {
getValues: function() {
return LData;
},
setValues: function (cid, email) {
LData.cid = cid;
LData.email = email;
}
};
});
Index.cshtml
同上。 HomeController.cshtml
:
var CID = null;
var Email = null;
$scope.submit = function() {
ShareUserInfo.setValues(d.data.CID, d.data.Email);
window.location.pathname = 'Home/OtherPage';
};
OtherPageController.js
:
$scope.values = ShareUserInfo.getValues();
OtherPage.cshtml
:
{{values}}
沒有正在輸出。記住兩個控制器,我將ShareUserInfo
作爲服務擴展傳遞給函數。
有什麼不對?爲什麼我的值沒有被存儲/傳遞?
所以你不要有一個單頁的應用程序?你可能可以將查詢字符串中的變量傳遞給其他人@PageHtml – Maccurt
@Maccurt我試圖避免查詢字符串選項與傳遞的電子郵件有關。 – NoReceipt4Panda