一家工厂由diff访问。控制器,但一次一个angularjs

时间:2017-02-15 05:49:30

标签: angularjs factory controllers

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

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

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

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

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

}]);

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

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


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

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

}]);

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

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


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

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

}]);

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(i){
	   				Precord.array.splice(i,1);
	   		}
		
	   		Precord.ADD = function(value){
	   				var newName = {
      					Name: value
    				};

    				Precord.array.push(newName);
	   		}

		return Precord;
}]);

如何通过不同的控制器访问单个工厂中的阵列,但一次只能访问一个控制器中的任何更新? precord.array正在所有控制器中使用,但我希望它与其他控制器隔离,而一个控制器正在使用它

1 个答案:

答案 0 :(得分: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;
    }]);