2013-04-07 8 views
0

내 응용 프로그램에서 하나의 컨트롤러에서 $ scope.needs.T를 업데이트합니다. 버튼을 클릭하면 다른 컨트롤러로 이동합니다. 거기에 $ scope.needs의 업데이트 된 값을 받고있다. 하지만 뒤로 버튼을 클릭하면 이전 컨트롤러로 이동합니다. 거기에 $ scope.needs의 업데이트 된 값을 얻지 못하고 있습니다. 이 문제를 해결하는 방법.

이것은 첫 번째 컨트롤러입니다. 다음은 $이 컨트롤러

$scope.increment = function(index) { 
    $scope.needs[index].commitment = parseFloat ($('#custom_commitment_'+index).val()) + 1000; 

    } 

에서

function CommitmentCtrl($scope, $routeParams, $http, UIData, cust, productList) { 
    $scope.needs = $scope.customer.priorities.list; 
} 

는 updated..rite을 scope.needs? 다음 버튼을 클릭하면 아래 함수가 호출됩니다.

$scope.gotoSummary = function(){ 
    $scope.customer.priorities.list = $scope.needs; 
} 

여기서 $ scope.customer.priorities.list에는 업데이트 된 값이 포함됩니다.

function SummaryCtrl($scope, $routeParams, $http, UIData, cust) { 
    $scope.$parent.needs = $scope.customer.priorities.list; 
} 

그런 다음 버튼을 첫 번째 컨트롤러를 호출하지만, $ 범위에서, 업데이트 된 값이 존재하지 않습니다 다시 클릭하여 다음 컨트롤러에게 있습니다.

두 위치에서 $ scope.needs를 어떻게 업데이트합니까?

+0

당신은 공유 할 수 주시겠습니까 fid –

답변

1

는 acheive하는 방법은 두 가지가 있습니다이 1) 당신은 만들 필요가

<html ng-app="jsf"> 
<head> 
    <title></title> 

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script> 
</head> 
<body> 
    <div ng-controller="CategoriesCtrl"> 
     <input type="button" value="update" ng-click="update()" /> 
    </div> 
     <div ng-controller="secondCtrl"> 
     <input type="button" value="check" ng-click="check()" /> 
    </div> 
    <script> 
     var app = angular.module('jsf', []); 


     app.controller('CategoriesCtrl', function ($scope, $rootScope) { 
      $rootScope.block = [3, 2, 1, 0]; 
      $scope.update = function() { 
       alert("123"); 
       $rootScope.block[1] = '5'; 
      } 
     }); 

     app.controller('secondCtrl', function ($scope, $rootScope) { 
      $scope.check = function() { 
       alert($rootScope.block[1]); 
      } 
     }); 
    </script> 
</body> 
</html> 

2) $ rootScope를 사용하여 각도 서비스

1)를 사용하여 $의 rootScope 2) 사용 각도 서비스 및 서비스 내에서 가치를 유지하고 컨트롤러를 통해 값을 공유

+0

서비스는 제안 # 1이어야합니다. '$ rootScope'는 데이터 저장에 사용되어서는 안됩니다. – charlietfl

관련 문제