如何从一个控制器angularjs将数据发送到其它 [英] How to send data from one controller to other in angularjs

查看:169
本文介绍了如何从一个控制器angularjs将数据发送到其它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何发送从FirstCtrl JSON数据使用angularjs到secondCtrl。

任何人都可以请帮我对此...

First.js:

  angular.module('对myApp')
    .controller('FirstCtrl',函数($范围){    // firstCtrl JSON数据
       $ .getJSON(sample.json功能(JSON){
            的console.log(JSON);
         });
    });

Second.js:

  angular.module('对myApp')
    .controller('secondCtrl',函数($范围){       //获取firstCtrl JSON数据在这里
    });


解决方案

我也建议,从和控制器获取并返回数据的服务。

我们创建两个控制器,然后我们创建了两个功能的服务:
1.一个拿到JSON数据
2.一个返回JSON数据

像这样:

 使用严格的;
angular.module('对myApp',[]).controller('FirstCtrl',函数($范围,为myService){
 //我们创建或获取JSON对象
 $ scope.myjsonObj = {
      '动物':'猫',
      '喂':'frieskies',
      '颜色':'白',
      '性别男'
      };      //传递JSON对象服务
      myService.setJson($ scope.myjsonObj);
}).controller('SecondCtrl',函数($范围,为myService){
        //呼叫服务的getJSON()函数来获取数据
       $ scope.myreturnedData = myService.getJson();
})
 .factory('为myService',函数(){
    VAR myjsonObj = NULL; //保存我们的数据的对象
     返回{
     的getJSON:功能(){
       返回myjsonObj;
     },
     setJson:函数(值){
      myjsonObj =价值;
     }
     }});

和HTML部分将是:

 < D​​IV NG-应用=对myApp>
        < D​​IV NG控制器=FirstCtrl>
          {{myjsonObj}}
        < / DIV>
        <小时>
        < D​​IV NG控制器=SecondCtrl>
            {{myreturnedData.animal}}
            {{myreturnedData.feed}}
            {{myreturnedData.color}}
            {{myreturnedData.sex}}
        < / DIV>

希望有所帮助,祝你好运。

How to send the json data from FirstCtrl to secondCtrl using angularjs.

Can anyone please help me out regarding this ...

First.js:

angular.module('myApp')
    .controller('FirstCtrl', function ($scope) {

    //firstCtrl json data
       $.getJSON("sample.json", function (json) {
            console.log(json);
         });
    });

Second.js:

angular.module('myApp')
    .controller('secondCtrl', function ($scope) {

       //get the firstCtrl json data here
    });

解决方案

I would also suggest a service that gets and returns data from and to the controllers.

we create the two controllers and then we create a service with two functions: 1. one to get the json data 2. one to return the json data

Like so:

'use strict';
angular.module('myApp', [])

.controller('FirstCtrl', function( $scope, myService ){
 //we create or get the json object
 $scope.myjsonObj = {
      'animal':'cat',
      'feed':'frieskies',
      'color':'white',
      'sex':'male'
      };

      //pass the json object to the service
      myService.setJson($scope.myjsonObj);
})

.controller('SecondCtrl', function( $scope, myService ){
        //call service getJson() function to get the data
       $scope.myreturnedData = myService.getJson();
})
 .factory('myService', function(){
    var myjsonObj = null;//the object to hold our data
     return {
     getJson:function(){
       return myjsonObj;
     },
     setJson:function(value){
      myjsonObj = value;
     }
     }

});

and the HTML partial would be:

 <div ng-app="myApp">
        <div ng-controller="FirstCtrl">
          {{myjsonObj}}
        </div>
        <hr>
        <div ng-controller="SecondCtrl">
            {{myreturnedData.animal}}
            {{myreturnedData.feed}}
            {{myreturnedData.color}}
            {{myreturnedData.sex}}
        </div>

Hope helps, good luck.

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

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