2013-04-07 77 views
0

在我的應用程序中,我從一個控制器更新$ scope.needs.Then當我單擊一個按鈕時,它將轉到另一個控制器。在那裏我得到$ scope.needs的更新值。但是,當點擊後退按鈕時,它將轉到前一個控制器。在那裏我沒有得到$ scope.needs的更新值。如何解決這個問題。

這是第一個控制器。

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

在此控制器

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

    } 

這裏$ scope.needs updated..rite?然後,當上按一下按鈕,下面的函數被調用

$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 dle – 2013-04-07 07:12:38

回答

1

有到acheive兩種方式這 1)使用$ rootScope 2)採用了棱角分明服務

1)使用$ rootScope

<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)你必須創建一個角度服務和維護服務內的價值並在控制器之間共享價值

+0

服務應該是建議#1。不應該使用'$ rootScope'來存儲數據 – charlietfl 2013-04-07 10:48:03