question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to set 'Page' level properties in angular component

See original GitHub issue

Hi! i’m using angular2-seed-advanced, web + mobile with angular+nativescript, i’m trying to set my background sitting under statusbar using opaqueTokens, but somehow it don’t work. My code: Token.ts

import {OpaqueToken} from '@angular/core';
export const PAGE: OpaqueToken = new OpaqueToken('page');

App.ts

nativeScriptBootstrap(NSAppComponent, [
    provide(WindowService, {useClass: WindowNative}),
    provide(PAGE, {useClass: Page}),
    ModalNative,
    provide(HttpService, {useClass: NSHttpService}),
    provide(TranslateLoader, {
        useFactory: () => {
            return new TNSTranslateLoader('assets/i18n');
        }
    }),
    NS_APP_PROVIDERS,
    nsProvideRouter(routes, {enableTracing: false})
]);

myComponent.ts

constructor(@Inject (PAGE) private page:PAGE) {
        if (CoreConfigService.IS_MOBILE_NATIVE) {
            console.log(this.page.ios);
            this.page.actionBarHidden = true;
            this.page.backgroundSpanUnderStatusBar=true;
            this.page.backgroundImage = this.page.ios ? "res://bg_login.jpg" : "res://bg_login";
        }
    }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
NathanWalkercommented, Aug 2, 2016

Just to conclude, turns out his issue was related to fact that when using an OpaqueToken to build a web/{N} shared codebase, one must provide the PAGE OpaqueToken in the correct way, for instance this works:

// somewhere in web codebase
import {OpaqueToken} from '@angular/core';
export const PAGE: any = new OpaqueToken('page');
...

// then only in the {N} app
nativeScriptBootstrap(AppComponent, [
    provide(PAGE, {
        useFactory: () => {
            const frame = topmost();
            if (frame) {
                return frame.currentPage;
            } else {
                return null;
            }
        }
    })
]);

... then in a component which is used on the web (as well as in {N} app), one can do this:

@Component({
    selector: 'any-cmp',
    templateUrl: 'any.component.html'
})
export class AnyComponent {

    constructor(@Inject(PAGE) private page:any) {
        if (Config.IS_MOBILE_NATIVE()) {
            this.page.actionBarHidden = true;
            this.page.backgroundSpanUnderStatusBar = true;
            this.page.backgroundImage = 'res://bg_login';
        }
    }

Works wonderfully and ensures that it doesn’t break the web 👍

1reaction
armackeycommented, Apr 28, 2019

@NathanWalker This method looks good but I haven’t been able to get this to work. Is provide a class I should import or create? Or has the standard changed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component styles - Angular
One way to do this is to set the styles property in the component metadata. The styles property takes an array of strings...
Read more >
Angular components overview
Navigate to your Angular project directory. · Create a new file, <component-name>. · At the top of the file, add the following import...
Read more >
Common Routing Tasks - Angular
For the component property, you can define any component in your application. Common choices include an application-specific PageNotFoundComponent , which you ...
Read more >
Property binding - Angular
To bind the src property of an <img> element to a component's property, place src in square brackets followed by an equal sign...
Read more >
Content projection - Angular
Create a component. In the template for your component, add an <ng-content> element where you want the projected content to appear. For ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found