这里我添加了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) {
});
答案 0 :(得分:1)
您可以将数据通过Rootscope或工厂或服务
JS使用工厂
<!doctype html>
<html ng-app="project">
<head>
<title>Angular: Service example</title>
<script src="https://code.angularjs.org/angular-1.0.1.js"></script>
<script>
var projectModule = angular.module('project',[]);
projectModule.factory('theService', function() {
var data ={}
data.x ={}
return data;
});
function FirstCtrl($scope, theService) {
console.log(theService)
$scope.name = "First Controller datas";
theService.x = $scope.name ;
}
function SecondCtrl($scope, theService) {
$scope.someThing = theService.x;
}
</script>
</head>
<body>
<div ng-controller="FirstCtrl">
<h2>{{name}}</h2>
</div>
<div ng-controller="SecondCtrl">
<h2> Second Controller recives {{someThing}}</h2>
</div>
</body>
</html>
答案 1 :(得分:0)
$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);});
});
答案 2 :(得分:0)
我修复你的代码,使用服务应该是一个正确的解决方案。
.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;
}
});