这里我添加了categoryCtrl和ProductCtrl。此代码行 console.log(category);
工作正常。如何使用 for 循环或其他方式为产品 Controller 绑定(bind)此数据。
.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 使用工厂
<!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>
关于javascript - 如何设置第一个 Controller 数据另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38563874/