如何设置第一个控制器数据另一个控制器 [英] How to set first controller data another controller

查看:136
本文介绍了如何设置第一个控制器数据另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我添加了categoryCtrl和ProductCtrl。此代码行 console.log(类别); 正常运行。如何使用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或工厂或服务

you can pass Datas either Rootscope or Factories or services

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>

这篇关于如何设置第一个控制器数据另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆