Angularjs: 如何将一个值从一个表单传递到另一个表单?
Angularjs: how do i pass a value from a form to a other form?
我需要从 table 获取 Id 并将其传递给控制器,我做了类似的事情但是每次更改表单时 id 值都会丢失,我想知道是否有一种方法,我添加了服务代码和控制器代码
//Here in get the IdValue
app.controller('SeguimientoSolicitudesController', ['$scope', 'ParametrosSolicitudes', function ($scope, ParametrosSolicitudes) {
this.SegSolic = "";
var self = this;
$scope.ValorId = function (value) {
ParametrosSolicitudes.setVarIdSolicitud(value);
window.location.href = urlServer + "Home/About";
};
solicitudContext.obtenerListaSegSolicitudes(function (resp) {
switch (resp.ressult) {
case "tgp":
self.SegSolic = solicitudContext.ListaSeguimientoSolicitudes;
break;
case "notgp":
break;
default:
break;
}
$scope.$apply();
});
}]);
//Here i get the detail of the id selected but the value is missing
app.controller('SolicitudesController', ['$scope', 'ParametrosSolicitudes', 'parametroConstante', function ($scope, ParametrosSolicitudes, parametroConstante) {
this.SolicitudDetalle = "";
var IdSolicitud = '';
var self = this;
$scope.$watch(function () { return ParametrosSolicitudes.getVarIdSolicitud() }, function () {
IdSolicitud = ParametrosService.getVarIdSolicitud();
});
solicitudContext.obtenerListaSolicitudes('R', IdSolicitud, function (resp) {
switch (resp.ressult) {
case "tgp":
self.SolicitudDetalle = solicitudContext.ListaSolicitudes;
break;
case "notgp":
break;
default:
break;
}
$scope.$apply();
});
}]);
Rafa,你有一个概念问题,当你重新加载页面时任何 JS 变量都会丢失,这意味着当你这样做时:
window.location.href = urlServer + "Home/About"
您正在丢失客户端的所有数据。
然后,要在 AngularJS 中从一个页面导航到另一个页面,您应该使用:$location 服务,在您的情况下类似于 $location.path("Home/About");
https://docs.angularjs.org/api/ng/service/$位置
检查这个样本
var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
controller: "SeguimientoSolicitudesController",
templateUrl: "SeguimientoSolicitudes.html"
})
.when('/details/:id/', {
controller: 'SolicitudesController',
templateUrl: "Solicitudes.html"
})
}]);
app.service("shareService", function(){
// lose this value if you reload the page, if you need it persistent you have to save to server or save to browser(cookies, localStorage)
var name = null;
return {
getName: function(){
return name;
},
setName: function(val){
name = val;
}
}
})
app.controller('SeguimientoSolicitudesController', ['$scope', '$location','shareService',function ($scope, $location, shareService) {
$scope.list = [{id: 5, name: "NameA"}, {id: 9, name: "NameB"}];
$scope.goToDetails = function(item){
shareService.setName(item.name);
$location.path("details/" + item.id);
}
}]);
app.controller('SolicitudesController', ['$scope','$routeParams','shareService',function ($scope, $routeParams, shareService) {
$scope.id = $routeParams.id;
$scope.name = shareService.getName();
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" ng-app="app">
<ng-view></ng-view>
<script type="text/ng-template" id="SeguimientoSolicitudes.html">
<p ng-repeat="item in list">
<a class="btn btn-link" ng-click="goToDetails(item)" style="cursor: pointer;">{{item.name}}</a>
</p>
</script>
<script type="text/ng-template" id="Solicitudes.html">
<p>ID: {{id}}, NAME: {{name}}</p>
<a class="btn btn-link" href="#/">back</a>
</script>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular-route.min.js"></script>
希望能帮到你
我需要从 table 获取 Id 并将其传递给控制器,我做了类似的事情但是每次更改表单时 id 值都会丢失,我想知道是否有一种方法,我添加了服务代码和控制器代码
//Here in get the IdValue
app.controller('SeguimientoSolicitudesController', ['$scope', 'ParametrosSolicitudes', function ($scope, ParametrosSolicitudes) {
this.SegSolic = "";
var self = this;
$scope.ValorId = function (value) {
ParametrosSolicitudes.setVarIdSolicitud(value);
window.location.href = urlServer + "Home/About";
};
solicitudContext.obtenerListaSegSolicitudes(function (resp) {
switch (resp.ressult) {
case "tgp":
self.SegSolic = solicitudContext.ListaSeguimientoSolicitudes;
break;
case "notgp":
break;
default:
break;
}
$scope.$apply();
});
}]);
//Here i get the detail of the id selected but the value is missing
app.controller('SolicitudesController', ['$scope', 'ParametrosSolicitudes', 'parametroConstante', function ($scope, ParametrosSolicitudes, parametroConstante) {
this.SolicitudDetalle = "";
var IdSolicitud = '';
var self = this;
$scope.$watch(function () { return ParametrosSolicitudes.getVarIdSolicitud() }, function () {
IdSolicitud = ParametrosService.getVarIdSolicitud();
});
solicitudContext.obtenerListaSolicitudes('R', IdSolicitud, function (resp) {
switch (resp.ressult) {
case "tgp":
self.SolicitudDetalle = solicitudContext.ListaSolicitudes;
break;
case "notgp":
break;
default:
break;
}
$scope.$apply();
});
}]);
Rafa,你有一个概念问题,当你重新加载页面时任何 JS 变量都会丢失,这意味着当你这样做时:
window.location.href = urlServer + "Home/About"
您正在丢失客户端的所有数据。
然后,要在 AngularJS 中从一个页面导航到另一个页面,您应该使用:$location 服务,在您的情况下类似于 $location.path("Home/About");
https://docs.angularjs.org/api/ng/service/$位置
检查这个样本
var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
controller: "SeguimientoSolicitudesController",
templateUrl: "SeguimientoSolicitudes.html"
})
.when('/details/:id/', {
controller: 'SolicitudesController',
templateUrl: "Solicitudes.html"
})
}]);
app.service("shareService", function(){
// lose this value if you reload the page, if you need it persistent you have to save to server or save to browser(cookies, localStorage)
var name = null;
return {
getName: function(){
return name;
},
setName: function(val){
name = val;
}
}
})
app.controller('SeguimientoSolicitudesController', ['$scope', '$location','shareService',function ($scope, $location, shareService) {
$scope.list = [{id: 5, name: "NameA"}, {id: 9, name: "NameB"}];
$scope.goToDetails = function(item){
shareService.setName(item.name);
$location.path("details/" + item.id);
}
}]);
app.controller('SolicitudesController', ['$scope','$routeParams','shareService',function ($scope, $routeParams, shareService) {
$scope.id = $routeParams.id;
$scope.name = shareService.getName();
}]);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" ng-app="app">
<ng-view></ng-view>
<script type="text/ng-template" id="SeguimientoSolicitudes.html">
<p ng-repeat="item in list">
<a class="btn btn-link" ng-click="goToDetails(item)" style="cursor: pointer;">{{item.name}}</a>
</p>
</script>
<script type="text/ng-template" id="Solicitudes.html">
<p>ID: {{id}}, NAME: {{name}}</p>
<a class="btn btn-link" href="#/">back</a>
</script>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular-route.min.js"></script>
希望能帮到你