Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71531

Share data between views from service like angular / ionic 1

$
0
0

@YourPrivateCoder wrote:

Hi all. I would be happy if someone can teach me right way to achive sharing data between components from service like in angular 1. As I understand "this" behaves now differently and it can't be handlet with var that = this; but I still want to hold my data in service and access this from different components just like in angular 1, is this possible?

For example I want to run app and initialize getMovies in app.js that sets this._movies value and I can access this value from whatever component/page with calling getMovie();

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';

@Injectable()
export class OrdersService {
    static get parameters() {
        return [[Http]];
    }

    constructor(http) {
        this.http = http;
        this._movies = [];
    }

    getMovies() {
        console.log(this);
        console.log(this._movies); // this._movies empty

        this.http.get('http://www.omdbapi.com/?t=interstellar&y=&plot=short&r=json').subscribe(res => {
            console.log(res._body); // prints movie data
            this._movies = res._body; // sets data that I want to access from other component
            console.log(this); // prints constructor
            console.log(this._movies); // prints this._movies from constructor, has the data from api
        });
    }


    getMovie() {
        console.log(this); // get's constructor
        console.log(this._movies); // this._movies have no value anymore
    }

}

Thank you.

Posts: 4

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 71531

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>