Javascript 在ASP MVC设置中,如何将AngularJS中的变量从一个控制器传递到下一个控制器?
例如,在Javascript 在ASP MVC设置中,如何将AngularJS中的变量从一个控制器传递到下一个控制器?,javascript,asp.net,angularjs,asp.net-mvc,Javascript,Asp.net,Angularjs,Asp.net Mvc,例如,在Views/Index.cshtml下,我有一个带有一些输入的表单,一旦填写并提交,就会通过控制器js文件中的一个函数,然后window.location.pathname重定向到另一个页面。下一页与另一个controller/js文件关联 如何将两个变量从一个传递到另一个 我试过这样做,但不起作用: 第一次尝试 \u Layout.cshtml: <html ng-app="MIScheduler"> <head> ... <
Views/Index.cshtml
下,我有一个带有一些输入的表单,一旦填写并提交,就会通过控制器js文件中的一个函数,然后window.location.pathname
重定向到另一个页面。下一页与另一个controller/js文件关联
如何将两个变量从一个传递到另一个
我试过这样做,但不起作用:
第一次尝试
\u Layout.cshtml
:
<html ng-app="MIScheduler">
<head>
...
</head>
<body>
@RenderBody()
// AngularJS scripts
<script src="app.js"></script>
</body>
</html>
<input ng-model="userID" name="userID" />
<input ng-model="userEmail" name="userEmail" />
<input type="submit" id="submit" />
@section scripts {
<script src="HomeController.js"></script>
}
{{cid}}
{{email}}
@section scripts {
<script src="OtherPageController.js"></script>
}
var CID = null;
var Email = null;
$scope.submit = function() {
ShareUserInfo.setValues(d.data.CID, d.data.Email);
window.location.pathname = 'Home/OtherPage';
};
{{values}}
HomeController.js
:
var CID = null;
var Email = null;
$scope.submit = function() {
CID = $scope.userID;
Email = $scope.userEmail;
window.location.pathname = 'Home/OtherPage';
};
$scope.cid = CID;
$scope.email = Email;
angular.module('moveinScheduler', [])
.service('ShareUserInfo', function () {
LData = {
cid: '',
email: ''
}
return {
getValues: function () {
return LData;
},
setValues: function (cid, email) {
LData.cid = cid;
LData.email = email;
}
};
});
$scope.values = ShareUserInfo.getValues();
然后当重定向到OtherPage.cshtml
:
<html ng-app="MIScheduler">
<head>
...
</head>
<body>
@RenderBody()
// AngularJS scripts
<script src="app.js"></script>
</body>
</html>
<input ng-model="userID" name="userID" />
<input ng-model="userEmail" name="userEmail" />
<input type="submit" id="submit" />
@section scripts {
<script src="HomeController.js"></script>
}
{{cid}}
{{email}}
@section scripts {
<script src="OtherPageController.js"></script>
}
var CID = null;
var Email = null;
$scope.submit = function() {
ShareUserInfo.setValues(d.data.CID, d.data.Email);
window.location.pathname = 'Home/OtherPage';
};
{{values}}
我还尝试在app.js
中实现一个服务,并将该服务传递给两个控制器,但没有任何输出也是这样
第二次尝试
这是我尝试的第二种方法:app.js
:
var CID = null;
var Email = null;
$scope.submit = function() {
CID = $scope.userID;
Email = $scope.userEmail;
window.location.pathname = 'Home/OtherPage';
};
$scope.cid = CID;
$scope.email = Email;
angular.module('moveinScheduler', [])
.service('ShareUserInfo', function () {
LData = {
cid: '',
email: ''
}
return {
getValues: function () {
return LData;
},
setValues: function (cid, email) {
LData.cid = cid;
LData.email = email;
}
};
});
$scope.values = ShareUserInfo.getValues();
Index.cshtml
与上述内容相同<代码>HomeController.cshtml:
<html ng-app="MIScheduler">
<head>
...
</head>
<body>
@RenderBody()
// AngularJS scripts
<script src="app.js"></script>
</body>
</html>
<input ng-model="userID" name="userID" />
<input ng-model="userEmail" name="userEmail" />
<input type="submit" id="submit" />
@section scripts {
<script src="HomeController.js"></script>
}
{{cid}}
{{email}}
@section scripts {
<script src="OtherPageController.js"></script>
}
var CID = null;
var Email = null;
$scope.submit = function() {
ShareUserInfo.setValues(d.data.CID, d.data.Email);
window.location.pathname = 'Home/OtherPage';
};
{{values}}
OtherPageController.js
:
var CID = null;
var Email = null;
$scope.submit = function() {
CID = $scope.userID;
Email = $scope.userEmail;
window.location.pathname = 'Home/OtherPage';
};
$scope.cid = CID;
$scope.email = Email;
angular.module('moveinScheduler', [])
.service('ShareUserInfo', function () {
LData = {
cid: '',
email: ''
}
return {
getValues: function () {
return LData;
},
setValues: function (cid, email) {
LData.cid = cid;
LData.email = email;
}
};
});
$scope.values = ShareUserInfo.getValues();
OtherPage.cshtml
:
<html ng-app="MIScheduler">
<head>
...
</head>
<body>
@RenderBody()
// AngularJS scripts
<script src="app.js"></script>
</body>
</html>
<input ng-model="userID" name="userID" />
<input ng-model="userEmail" name="userEmail" />
<input type="submit" id="submit" />
@section scripts {
<script src="HomeController.js"></script>
}
{{cid}}
{{email}}
@section scripts {
<script src="OtherPageController.js"></script>
}
var CID = null;
var Email = null;
$scope.submit = function() {
ShareUserInfo.setValues(d.data.CID, d.data.Email);
window.location.pathname = 'Home/OtherPage';
};
{{values}}
没有输出任何内容。请记住,对于这两个控制器,我将把ShareUserInfo
作为服务扩展传递到函数中
有什么问题吗?为什么不存储/传递我的值?由于您不使用单页应用程序,并且避免使用查询字符串,因此可以使用$http.post,并将视图模型对象传递给OtherPageController。在OtherPageController中,实现一个[HttpPost]操作方法,该方法获取从$http.post调用传入的视图模型对象。您可以将数据保存在: HomeController.js:
angular.controller('ExampleController', function($scope) {
$scope.submit = function() {
var obj = {
cid: $scope.userID,
email: $scope.userEmail
};
localStorage.setItem('data', JSON.stringify(obj));
// Make your redirect
};
});
angular.controller('PageController', function($scope) {
var data = JSON.parse(localStorage.getItem('data'));
console.log('data: ', data);
});
PageController.js:
angular.controller('ExampleController', function($scope) {
$scope.submit = function() {
var obj = {
cid: $scope.userID,
email: $scope.userEmail
};
localStorage.setItem('data', JSON.stringify(obj));
// Make your redirect
};
});
angular.controller('PageController', function($scope) {
var data = JSON.parse(localStorage.getItem('data'));
console.log('data: ', data);
});
所以你没有一个单页应用程序?您是否可以将querystring中的变量传递给othersPage。html@Maccurt对于传入的电子邮件,我试图避免使用querystring选项。
['ngStrorage']
需要放在所有的js文件中,包括app.js
对吗?我已经删除了对ngStorage
的依赖,以使它更简单。。现在您可以直接在localStorage
中保存数据了。我有一个问题,我完全按照上面的代码所述做了。在HomeController.js
中,一旦执行未定义$localStorage的函数,就会出现一个错误。但是如果我将$localStorage
放入..('HomeController',函数($scope,$localStorage)..
代码中断,我收到错误:$injector:unpr未知提供程序请尝试更新响应..您收到错误的原因是您必须首先安装模块ngStorage
并需要ngStorage并在控制器中插入服务..这太奇怪了。控制台是空的。在我的subm中它是一个函数,我重定向到另一个页面的方式是:window.location.pathname='Home/OtherPage';
(我在submit函数的末尾有这个),这就是它不显示的原因吗?