简体   繁体   English

Angular js,从服务传递对象

[英]Angular js , passing object from service

'use strict';

var app = angular.module('app');
app.factory('currTripService', function() {
      var currtrip ='';

    return{
        setCurrTrip: function(trip){ 
            currtrip = trip ;
        },
        getCurrTrip: function(){ 
            return currtrip ;
        },
    }
}); 


app.controller('TripCreateController', function($scope, $location, Trip,currTripService) {
    //The save method which is called when the user wants to submit their data
    $scope.save = function() {

        //Create the forum object to send to the back-end
        var trip = new Trip($scope.trip);
          console.log(trip);
         currTripService.setCurrTrip(trip);
         console.log(currTripService.getCurrTrip());
        //Save the forum object
        trip.$save(function() {
            //Redirect us back to the main page
            $location.path('/trip/day/1');

        }, function(response) {

            //Post response objects to the view
            $scope.errors = response.data.errors;
        });
    }
});

app.controller('TripDayCreateController',function($scope,$routeParams,currTripService){
    $scope.items=[];
    $scope.trip = currTripService.getCurrTrip();
 console.log($scope.trip.city);

    // $scope.products = productService.getProducts();
    $scope.addItem = function(item) {
                $scope.items.push(item);
                $scope.item = {};
    }
});

When i click on /trip/new , its does the save in TripCreateController and set the trip object inside currTripService. 当我点击/ trip / new时,它会在TripCreateController中进行保存并在currTripService中设置trip对象。 Then when redirected to TripDayCreateContoller the console.log(currTripService.getTrip()) , returns 'undefined' 然后当重定向到TripDayCreateContoller时, console.log(currTripService.getTrip())返回'undefined'

Is it because Trip is an object ? 是因为Trip是一个对象吗? How can i fix this ? 我怎样才能解决这个问题 ?

try this: 尝试这个:

app.factory('currTripService', function() {
  var currtrip = '';
  var self = this;
  return{
    setCurrTrip: function(trip){ 
        self.currtrip = trip ;
    },
    getCurrTrip: function(){ 
        return self.currtrip ;
    },
  }
}); 

When you declare a function, this scope changes so currtrip was only existing in your getter/setter functions, but not outside. 声明函数时, this范围会发生变化,因此currtrip仅存在于getter / setter函数中,但不存在于外部。

The best way to do this is to use a class. 最好的方法是使用一个类。 Below is a an example of a class from CoffeeScript. 下面是CoffeeScript的一个类的示例。

class currTripService

    # storage object
    @data = null

    # get data
    get: =>
        return @data

    # set data
    put: (data) =>
       @data = data

app.factory('currTripService', currTripService)

However if you want to do this without a class method then you can instead use something that would imitate a class: 但是,如果你想在没有类方法的情况下这样做,那么你可以使用一些模仿类的东西:

var currTripService = function () {

    // storage variable
    var currTrip = null

    // reference to this element
    var _this = this

    return{
        // set this trip value
        setCurrTrip: function(trip){ 
            _this.currtrip = trip;
        },
        // get this trip value
        getCurrTrip: function(){ 
            return _this.currtrip;
        },
    }
}

app.factory('currTripService', currTripService);

Just a note: I put the function outside the factory to imitate how you'd typically call a class, but you can obviously just put all of the code in the function declaration. 请注意:我将函数放在工厂外面来模仿你通常调用类的方式,但显然你可以将所有代码放在函数声明中。

app.factory('currTripService', function () {

    // logic

});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM