使用Angular获取要在其他页面中编辑的值

时间:2016-05-12 19:59:04

标签: angularjs

我想在其他页面中编辑值

<tr ng-repeat="facilitator in listFacilitators"  ng-click="showDetails(facilitator);goToFacilitatorP()"> 
    <td>{{facilitator.username}}</td> 
    <td><li><ul>{{facilitator.cwsessionsAnime}}</ul></li></td>
    <td><li><ul>{{facilitator.profilFollow}}</ul></li></td> 
</tr>

当我点击“我想要在另一个页面中重定向以显示详细信息并编辑它们时,我试图在控制器中执行此操作:

$scope.showDetails= function(facilitator){
    $scope.selectedFac= facilitator;
}

在第二页中,我这样做:

<tr>
    <td> {{selectedFac.username}}</td>
    <td> {{selectedFac.lastname}}</td>
    <td> {{selectedFac.firstname}}</td>
    <td> {{selectedFac.title}}</td>
</tr>

它在同一页面中有效,但在我重定向时,请不要帮助我吗?

UPDATE: 

我这样做但是我仍然没有在seconde页面中提供数据: 1-在我宣布的1页的第一个控制器中:

profilCtrl.controller('ProfilCtrl', [ '$scope','$location', 'profilService', 'facilitatorPService', function($scope,$location, profilService, facilitatorPService) {
/* ---- appel a facilitator Service ---- */
var facilitator = '';// What ever this is set to in the first place
facilitatorPService.facilitator = facilitator;

2-在我的第二个控制器中(我想要显示细节的第二个),我已声明:

facilitatorPCtrl.controller('facilitatorPCtrl', [ '$scope','$rootScope','$cookieStore','$location','membreService','facilitatorPService','userService',function($scope,$rootScope,$cookieStore,$location, membreService,facilitatorPService, userService) {
facilitatorPservice.editFacil= function($scope){
    $scope.showDetails = function(){
        $scope.selectedFac = facilitatorPService.facilitator;
    }
};

3- Ans在我的服务主持人PS服务中我有这个:

facilitatorPService.factory('facilitatorPService', [ '$resource','$http', function($resource,$http) {
    var service = {                 
            getAllFacilitators : function($scope){  
                return $resource('/gari-web/services/facilitators/AllFacilitators', {}, {
                    query : {
                        method : 'GET', isArray:true,
                         }}
                });
            }, 
            editFacil: function($scope){
                var self= this;
                self.facilitator={};
            }};
    return service;
} ]);

4-在我的html页面中我把它:

<td>{{selectedFac.username}}</td>

有人可以告诉我我做错了什么,我没有发现错误

2 个答案:

答案 0 :(得分:1)

控制器被刷新&#39;当你改变观点。要将数据从视图保留到另一个视图,请将数据存储在服务

<强>更新

 .service('FacilitatorService', [
        function() {
            var self = this;
            self.facilitator = {};
        }
    ])

然后在您的控制器中,为您自己注入刚刚创建的服务。

 .controller('FirstController', ['FacilitatorService',
        function(FacilitatorService) {
            var facilitator = '';// What ever this is set to in the first place
            FacilitatorService.facilitator = facilitator;
        }
    ])

在你的第二个控制器中

 .controller('SecondController', ['FacilitatorService', '$scope',
        function(FacilitatorService, $scope) {
            $scope.showDetails = function(){
                $scope.selectedFac = FacilitatorService.facilitator;
            }
        }
    ])

像这样,您的所有使用FacilitatorService.facilitator

的控制器都可以访问您的FacilitatorService数据

答案 1 :(得分:0)

我建议使用ui路由器,这解决了两个问题,即视图更改和数据共享。

详细了解路由器ui

doc http://angular-ui.github.io/ui-router/site/#/api/ui.router

当前状态

$stateProvider
  .state('current', {
    url: "/curent",
    templateUrl: 'current.html',
    controller: 'currentCtrl'
  })

然后你可以选择改变视图和共享数据的方式,第一个是控制器,第二个是html。只使用一个

来自当前控制器(选项1)

$scope.showDetails= function(facilitator){
    $state.go('togo', {myCurrentdata: facilitator}) ;
}

从当前的html(option2)

<tr ng-repeat="facilitator in listFacilitators" ui-sref="togo({myCurrentdata : facilitator})"> 
    <td>{{facilitator.username}}</td> 
    <td><li><ul>{{facilitator.cwsessionsAnime}}</ul></li></td>
    <td><li><ul>{{facilitator.profilFollow}}</ul></li></td> 
</tr>

你想去的州

$stateProvider
  .state('togo', {
    url: "/togo",
    templateUrl: 'togo.html',
    controller: 'togoCtrl'
    param: {myCurrentdata: null}
  })

去控制器

if($stateParams.myCurrentdata){
   $scope.selectedFac = $stateParams.myCurrentdata
}