2017-02-15 49 views
0

差異化訪問的一家工廠。控制器但一次一個angularjs

var app=angular.module('myApp',[]); 
 

 
app.controller('FCtrl',['$scope','mockFactory',function($scope,mockFactory){ 
 

 
\t $scope.showPerson = function(){ 
 
\t \t mockFactory.fetchJson($scope.valueJson) 
 
\t \t .then(function(){ 
 
\t \t $scope.persons = mockFactory.array; 
 
\t \t }) 
 
\t } 
 

 
    \t $scope.delPerson = function(i){ 
 
    \t \t mockFactory.delete(i); 
 
\t } 
 

 
\t $scope.addNamePerson = function() { 
 
    \t \t mockFactory.ADD($scope.valueFirst); 
 
    \t \t }; 
 
\t 
 
    \t $scope.showConsolePerson= function(){ 
 
    \t \t console.log(JSON.stringify(mockFactory.array)); 
 
    \t } 
 

 
}]); 
 

 
app.controller('SCtrl',['$scope','mockFactory',function($scope,mockFactory){ 
 

 
\t $scope.showMovie = function(){ 
 
\t \t mockFactory.fetchJson($scope.valueJson) 
 
\t \t .then(function(){ 
 
\t \t $scope.movies = mockFactory.array; 
 
\t \t }) 
 
\t } 
 

 

 
    \t $scope.delMovie = function(i){ 
 
    \t \t mockFactory.delete(i); 
 
\t } 
 

 
\t $scope.addNameMovie = function() { 
 
    \t \t mockFactory.ADD($scope.valueSecond); 
 
    \t \t }; 
 
\t 
 
    \t $scope.showConsoleMovie= function(){ 
 
    \t \t console.log(JSON.stringify(mockFactory.array)); 
 
    \t } 
 

 
}]); 
 

 
app.controller('TCtrl',['$scope','mockFactory',function($scope,mockFactory){ 
 

 
\t $scope.showPlace = function(){ 
 
\t \t mockFactory.fetchJson($scope.valueJson) 
 
\t \t .then(function(){ 
 
\t \t $scope.places = mockFactory.array; 
 
\t \t }) 
 
\t } 
 

 

 
    \t $scope.delPlace = function(i){ 
 
    \t \t mockFactory.delete(i); 
 
\t } 
 

 
\t $scope.addNamePlace = function() { 
 
    \t \t mockFactory.ADD($scope.valueThird); 
 
    \t \t }; 
 
\t 
 
    \t $scope.showConsolePlace= function(){ 
 
    \t \t console.log(JSON.stringify(mockFactory.array)); 
 
    \t } 
 

 
}]); 
 

 
app.factory('mockFactory',['$http',function($http){ 
 
\t \t 
 
\t \t var Precord = {}; 
 

 
\t \t \t Precord.array = []; 
 
\t \t \t Precord.assign = function (value) { 
 
\t   \t return $http.get('http://localhost:3000/scripts/' + value + '.json'); 
 
\t  \t }; 
 
\t  \t Precord.fetchJson = function(value){ 
 
\t  \t \t return Precord.assign(value).success(function(response){ 
 
\t \t \t \t \t Precord.array = response.value; 
 
\t \t \t \t \t 
 
\t \t \t \t }) 
 
\t  \t } 
 

 
\t  \t Precord.delete = function(i){ 
 
\t  \t \t \t \t Precord.array.splice(i,1); 
 
\t  \t \t } 
 
\t \t 
 
\t  \t \t Precord.ADD = function(value){ 
 
\t  \t \t \t \t var newName = { 
 
     \t \t \t \t \t Name: value 
 
    \t \t \t \t }; 
 

 
    \t \t \t \t Precord.array.push(newName); 
 
\t  \t \t } 
 

 
\t \t return Precord; 
 
}]);

如何在單個工廠陣列由不同的控制器來訪問,但一次一個,使得在一個控制器的任何更新不能在其他控制器反映? precord.array正在所有的控制器中使用,但我希望它是從其他控制器隔離,而一個控制器正在使用它

+0

保持副本相應的控制器使用 '角,複製()' –

+0

能否請你看上面的腳本和說得清使用angular.copy ...任何幫助,將不勝感激 –

回答

0

在審查你的代碼後,我發現你應該保持控制器上的數組副本因此,如果一個控制器更新它,那麼它將不會反映在其他控制器中,我也修改了您的一個控制器和您的工廠,所以嘗試在其他控制器中實現它。

試試這個

FCTRL

var app=angular.module('myApp',[]); 

    app.controller('FCtrl',['$scope','mockFactory',function($scope,mockFactory){ 

     $scope.fCtrlJSON = [];   

     $scope.showPerson = function(){ 
      mockFactory.fetchJson($scope.valueJson) 
      .then(function(){ 
       $scope.persons = mockFactory.array; 
       $scope.fCtrlJSON = mockFactory.array;   
      }) 
     } 

     $scope.delPerson = function(i){ 
      mockFactory.delete($scope.fCtrlJSON,i); 
     } 

     $scope.addNamePerson = function() { 
      mockFactory.ADD($scope.fCtrlJSON,$scope.valueFirst); 
     }; 

     $scope.showConsolePerson= function(){ 
      console.log(JSON.stringify($scope.fCtrlJSON)); 
     } 

    }]); 

mockFactory

app.factory('mockFactory',['$http',function($http){ 

     var Precord = {}; 

     Precord.array = []; 
     Precord.assign = function (value) { 
      return $http.get('http://localhost:3000/scripts/' + value + '.json'); 
     }; 
     Precord.fetchJson = function(value){ 
      return Precord.assign(value).success(function(response){ 
       Precord.array = response.value;     
      }) 
     } 

     Precord.delete = function(arrayData, i){ 
      arrayData.splice(i,1); 
     } 

     Precord.ADD = function(arrayData, value){ 
      var newName = { 
       Name: value 
      }; 
      arrayData.push(newName); 
     } 

     return Precord; 
    }]); 
+0

謝謝很多老兄Saurabh Agrawal ...它的工作! –

+0

享受:) @SauravDutta。標記它也:) –

+0

@Saureabh Agrawal ...其實我是新來的stackoverflow,所以它說少於15聲譽不會讓我upvote ....其唯一的記錄,但不顯示 –

相關問題