【发布时间】:2016-07-18 14:49:53
【问题描述】:
例如,在Views/Index.cshtml 下,我有一个带有一些输入的表单,一旦填写并提交,就会通过控制器 js 文件中的一个函数,然后将window.location.pathname 重定向到另一个页面。下一页与另一个控制器/js 文件相关联。
如何将几个变量从一个传递到另一个?
我尝试过这样做,但它不起作用:
第一次尝试
_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 作为服务扩展传递给函数。
有什么想法吗?为什么我的值没有被存储/传递?
【问题讨论】:
-
所以你没有单页应用?您能否将查询字符串中的变量传递给 othersPage.html
-
@Maccurt 对于传入的电子邮件,我试图避免使用查询字符串选项。
标签: javascript asp.net angularjs asp.net-mvc