如何设置第一个控制器数据另一个控制器
这里我添加了categoryCtrl和ProductCtrl。 此代码行 console.log(category);
工作正常。 如何使用for循环或其他任何方式为产品控制器绑定此数据。
.controller('CategoryCtrl', function($scope, $http) { var vm = this; vm.playlists = {}; $http.get("http://localhost/youtubewebservice/shop-categorylist-product.php").then(function(response) { vm.playlists = response.data.category; console.log(response.data.category); }); $scope.selectedJsonObject=function(category){ console.log(category); } }) .controller('productCtrl', function($scope, $stateParams) { });
您可以通过数据采集Rootscope或工厂或服务
JS使用工厂
Angular: Service example {{name}}
Second Controller recives {{someThing}}
我修复你的代码,使用服务应该是一个正确的解决方案。
.controller('CategoryCtrl', function($scope, myService) { var vm = this; vm.playlists = myService.playList; $scope.selectedJsonObject=function(category){ console.log(category); } }) .controller('productCtrl', function($scope, $stateParams, myService) { this.playlists = myService.playList; }); .service('myService', function($http){ var playlist = {}; $http.get("http://localhost/youtubewebservice/shop-categorylist-product.php").then(function(response) { playlist = response.data.category; console.log(response.data.category); }); this.playList = function(){ return playList; } });
$scope.selectedJsonObject=function(category){ console.log(category); $scope.$emit('eventName', { message: category}); }
并在您的productctrl使用
.controller('productCtrl', function($scope, $stateParams) { $scope.$on('eventName', function (event, args) { $scope.message = args.message; console.log($scope.message);}); });