简体   繁体   English

使用工厂将数据从一个控制器传递到AngularJS中的另一个控制器

[英]Using a factory to pass data from one controller to another controller in AngularJS

I'm trying to take in the longitude and latitude values (using the google maps API) from one controller, and sending it to another controller. 我正在尝试从一个控制器获取经度和纬度值(使用google maps API),然后将其发送到另一个控制器。 To do this, I'm using a factory. 为此,我正在使用工厂。 However, there seems to be a problem with either sending the information into the factory, or reading from it. 但是,将信息发送到工厂或从工厂读取似乎存在问题。

.factory('loc', function(){
var location = {};

return {
setProperty: function(latitude, longitude){
  location.lat = latitude;
  location.lng = longitude;
},
getProperty: function(){
  return location;
}
};
});

The first controller (google maps) 第一个控制器(谷歌地图)

.controller('MapCtrl', function($scope, $ionicLoading,loc) {

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(function(pos) {
loc.setProperty(pos.coords.latitude, pos.coords.longitude) 
...

In the second controller, I have: 在第二个控制器中,我有:

.controller('callCtrl', function($scope,$state,loc) {
$scope.updateTask = function(data) {

  task.location_lng = loc.getProperty().lng;
  task.location_lat = loc.getProperty().lat;
...

See running code here... http://play.ionic.io/app/a24d56659129 在此处查看运行代码... http://play.ionic.io/app/a24d56659129

html html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <link href="https://code.ionicframework.com/1.0.0/css/ionic.min.css" rel="stylesheet">
    <script src="https://code.ionicframework.com/1.0.0/js/ionic.bundle.js"></script>
  </head>
  <body ng-app="app">
    <ion-pane ng-controller="MainCtrl">
      <ion-header-bar class="bar-stable">
        <h1 class="title">Awesome App</h1>
      </ion-header-bar>
      <ion-content class="padding">
        <button class="button button-assertive" ng-click="setLocation()">Set Property</button><br/><br/>
        <button class="button button-assertive" ng-click="getLocation()">Get Property</button><br/><br/>
        <button class="button button-assertive" ng-click="clearLocation()">Clear Property</button>
      </ion-content>
    </ion-pane>
  </body>
</html>

app.js app.js

var app = angular.module('app', ['ionic']);

app.controller('MainCtrl', function($scope,loc) {

  //

  $scope.setLocation = function() {
    loc.setProperty( 100, 150)
  }


  $scope.getLocation = function() {
   alert(JSON.stringify(loc.getProperty()));
  }

  $scope.clearLocation = function() {
    loc.setProperty( "", "")
  }


});

app.factory('loc', function() {
  var location = {};

  return {
    setProperty: function(latitude, longitude){
      location.lat = latitude;
      location.lng = longitude;
    },
    getProperty: function(){
      return location;
    }
  };
});

More complete example here http://play.ionic.io/app/f4332405a2c1 更完整的示例在这里http://play.ionic.io/app/f4332405a2c1

This Works: 这有效:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope,loc) {
  loc.setProperty(10,20); 
});


app.controller('SubCtrl', function($scope,loc) {

  $scope.updateData = function(){
    //this is the change
    $scope.lat = loc.getProperty().lat;
    $scope.lng = loc.getProperty().lng;
  }
});

app.factory('loc', function() {
  var location = {};

  return {
    setProperty: function(latitude, longitude) {
      location.lat = latitude;
      location.lng = longitude;
    },
    getProperty: function() {
      return location;
    }
  };
});

Demo : http://plnkr.co/edit/aWEEEjGmsnwMMtAAUZKV?p=preview 演示: http : //plnkr.co/edit/aWEEEjGmsnwMMtAAUZKV?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Angularjs中使用Factory从一个控制器调用函数到另一个控制器 - Call function from one controller to another controller using Factory in Angularjs 如何在angularjs中将对象数据从一个控制器传递到另一个控制器 - how to pass object data from one controller to another controller in angularjs AngularJS-无法将数据从工厂传递到控制器 - Angularjs - cannot pass data from factory to controller 将数组数据从AngularJS中的控制器传递到工厂 - Pass Array Data to factory from controller in AngularJS AngularJS将数据从控制器传递到另一个控制器 - AngularJS pass data from controller to another controller AngularJS - 使用服务工厂使用来自另一个控制器的$ resource更新数据 - AngularJS - update data using services factory with $resource from another controller AngularJS:在没有工厂,服务或广播的情况下将数据从控制器传递到控制器? - AngularJS : pass data from controller to controller without factory, service or broadcast? AngularJS:使用同一控制器将数据从一个视图传递到另一个视图 - AngularJS : Pass data from one view to another with in same controller 通过AngularJS中的控制器将结果从一个工厂/服务ajax方法传递给另一个工厂/服务ajax方法 - Pass results from one factory/service ajax method to another via the controller in AngularJS 如何在angularjs中将登录数据从一个控制器传递到另一个控制器而不是使用rootscope - how to pass login data from one controller to another other than using rootscope, in angularjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM