簡體   English   中英

使用工廠將數據從一個控制器傳遞到AngularJS中的另一個控制器

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

我正在嘗試從一個控制器獲取經度和緯度值(使用google maps API),然后將其發送到另一個控制器。 為此,我正在使用工廠。 但是,將信息發送到工廠或從工廠讀取似乎存在問題。

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

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

第一個控制器(谷歌地圖)

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

if (navigator.geolocation) {

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

在第二個控制器中,我有:

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

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

在此處查看運行代碼... http://play.ionic.io/app/a24d56659129

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

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;
    }
  };
});

更完整的示例在這里http://play.ionic.io/app/f4332405a2c1

這有效:

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;
    }
  };
});

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM