Angular 7 + NG Bootstrap, open modal from a Service?
i'm kinda new with Angular, im trying to make a service that checks localstorage and if the desired key isnt there open a Modal asking for a name to create that localstorage key.
However, the modal opens but I only see the "disabled" background and not the modal info.
I dont know what I'm doing wrong and I couldnt find a lot of info about this.
Dont know if a service its the right way to do this and I'd like some help with this.
Also, service depending on another services its a good practice?
Here's the code:
import { Injectable } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { localStorageService } from './localStorage.service';
let template: `<ng-template #content let-modal let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Insert Your Name</h4>
</div>
<div class="modal-body">
<form>
<div class="input-group">
<input [(ngModel)]='name' id="name" class="form-control" name="name">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="saveName(name);c('Save Click')">Save</button>
</div>
</ng-template>`
@Injectable({
providedIn: 'root'
})
export class CheckuserService {
private closeResult: String;
constructor(private modalService: NgbModal, private lss: localStorageService, ) { }
test() {
if (this.lss.getStorage() !== "null") {
this.modalService.open(template, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
});
} else {
console.log("Already logged")
}
}
}
angular ng-bootstrap angular7
|
show 2 more comments
i'm kinda new with Angular, im trying to make a service that checks localstorage and if the desired key isnt there open a Modal asking for a name to create that localstorage key.
However, the modal opens but I only see the "disabled" background and not the modal info.
I dont know what I'm doing wrong and I couldnt find a lot of info about this.
Dont know if a service its the right way to do this and I'd like some help with this.
Also, service depending on another services its a good practice?
Here's the code:
import { Injectable } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { localStorageService } from './localStorage.service';
let template: `<ng-template #content let-modal let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Insert Your Name</h4>
</div>
<div class="modal-body">
<form>
<div class="input-group">
<input [(ngModel)]='name' id="name" class="form-control" name="name">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="saveName(name);c('Save Click')">Save</button>
</div>
</ng-template>`
@Injectable({
providedIn: 'root'
})
export class CheckuserService {
private closeResult: String;
constructor(private modalService: NgbModal, private lss: localStorageService, ) { }
test() {
if (this.lss.getStorage() !== "null") {
this.modalService.open(template, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
});
} else {
console.log("Already logged")
}
}
}
angular ng-bootstrap angular7
Not related, but consider changing yourvar
tolet
orconst
, in your template. Is a better practice.
– dream88
Nov 12 '18 at 13:54
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09
|
show 2 more comments
i'm kinda new with Angular, im trying to make a service that checks localstorage and if the desired key isnt there open a Modal asking for a name to create that localstorage key.
However, the modal opens but I only see the "disabled" background and not the modal info.
I dont know what I'm doing wrong and I couldnt find a lot of info about this.
Dont know if a service its the right way to do this and I'd like some help with this.
Also, service depending on another services its a good practice?
Here's the code:
import { Injectable } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { localStorageService } from './localStorage.service';
let template: `<ng-template #content let-modal let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Insert Your Name</h4>
</div>
<div class="modal-body">
<form>
<div class="input-group">
<input [(ngModel)]='name' id="name" class="form-control" name="name">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="saveName(name);c('Save Click')">Save</button>
</div>
</ng-template>`
@Injectable({
providedIn: 'root'
})
export class CheckuserService {
private closeResult: String;
constructor(private modalService: NgbModal, private lss: localStorageService, ) { }
test() {
if (this.lss.getStorage() !== "null") {
this.modalService.open(template, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
});
} else {
console.log("Already logged")
}
}
}
angular ng-bootstrap angular7
i'm kinda new with Angular, im trying to make a service that checks localstorage and if the desired key isnt there open a Modal asking for a name to create that localstorage key.
However, the modal opens but I only see the "disabled" background and not the modal info.
I dont know what I'm doing wrong and I couldnt find a lot of info about this.
Dont know if a service its the right way to do this and I'd like some help with this.
Also, service depending on another services its a good practice?
Here's the code:
import { Injectable } from '@angular/core';
import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap';
import { localStorageService } from './localStorage.service';
let template: `<ng-template #content let-modal let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Insert Your Name</h4>
</div>
<div class="modal-body">
<form>
<div class="input-group">
<input [(ngModel)]='name' id="name" class="form-control" name="name">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-dark" (click)="saveName(name);c('Save Click')">Save</button>
</div>
</ng-template>`
@Injectable({
providedIn: 'root'
})
export class CheckuserService {
private closeResult: String;
constructor(private modalService: NgbModal, private lss: localStorageService, ) { }
test() {
if (this.lss.getStorage() !== "null") {
this.modalService.open(template, { ariaLabelledBy: 'modal-basic-title' }).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
});
} else {
console.log("Already logged")
}
}
}
angular ng-bootstrap angular7
angular ng-bootstrap angular7
edited Nov 15 '18 at 9:28
Goncalo Peres
1,3261318
1,3261318
asked Nov 12 '18 at 13:51
Maty
67
67
Not related, but consider changing yourvar
tolet
orconst
, in your template. Is a better practice.
– dream88
Nov 12 '18 at 13:54
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09
|
show 2 more comments
Not related, but consider changing yourvar
tolet
orconst
, in your template. Is a better practice.
– dream88
Nov 12 '18 at 13:54
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09
Not related, but consider changing your
var
to let
or const
, in your template. Is a better practice.– dream88
Nov 12 '18 at 13:54
Not related, but consider changing your
var
to let
or const
, in your template. Is a better practice.– dream88
Nov 12 '18 at 13:54
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09
|
show 2 more comments
1 Answer
1
active
oldest
votes
Sorry - I did not notice in my comment that you open the template from the service and the template is a string.
If you look into the documentation of NgBoostrap - it opens a TemplateRef or a Component type - not a string.
open open(content: any, options: NgbModalOptions) => NgbModalRef
Opens a new modal window with the specified content and using supplied
options. Content can be provided as a TemplateRef or a component type.
If you pass a component type as content than instances of those
components can be injected with an instance of the NgbActiveModal
class. You can use methods on the NgbActiveModal class to close /
dismiss modals from "inside" of a component.
When you reference an <ng-template>
inside a component likeViewChild('tplRef', {read: TemplateRef}) myTplRef: TemplateRef;
It creates a TemplateRef object (which you later pass to NgBootstrap modal service).
What you are trying to do on the other hand is to pass a string - which NgBootstrap does not support.
Therefore, you should pass the template reference / component as a parameter when calling your service.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263616%2fangular-7-ng-bootstrap-open-modal-from-a-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sorry - I did not notice in my comment that you open the template from the service and the template is a string.
If you look into the documentation of NgBoostrap - it opens a TemplateRef or a Component type - not a string.
open open(content: any, options: NgbModalOptions) => NgbModalRef
Opens a new modal window with the specified content and using supplied
options. Content can be provided as a TemplateRef or a component type.
If you pass a component type as content than instances of those
components can be injected with an instance of the NgbActiveModal
class. You can use methods on the NgbActiveModal class to close /
dismiss modals from "inside" of a component.
When you reference an <ng-template>
inside a component likeViewChild('tplRef', {read: TemplateRef}) myTplRef: TemplateRef;
It creates a TemplateRef object (which you later pass to NgBootstrap modal service).
What you are trying to do on the other hand is to pass a string - which NgBootstrap does not support.
Therefore, you should pass the template reference / component as a parameter when calling your service.
add a comment |
Sorry - I did not notice in my comment that you open the template from the service and the template is a string.
If you look into the documentation of NgBoostrap - it opens a TemplateRef or a Component type - not a string.
open open(content: any, options: NgbModalOptions) => NgbModalRef
Opens a new modal window with the specified content and using supplied
options. Content can be provided as a TemplateRef or a component type.
If you pass a component type as content than instances of those
components can be injected with an instance of the NgbActiveModal
class. You can use methods on the NgbActiveModal class to close /
dismiss modals from "inside" of a component.
When you reference an <ng-template>
inside a component likeViewChild('tplRef', {read: TemplateRef}) myTplRef: TemplateRef;
It creates a TemplateRef object (which you later pass to NgBootstrap modal service).
What you are trying to do on the other hand is to pass a string - which NgBootstrap does not support.
Therefore, you should pass the template reference / component as a parameter when calling your service.
add a comment |
Sorry - I did not notice in my comment that you open the template from the service and the template is a string.
If you look into the documentation of NgBoostrap - it opens a TemplateRef or a Component type - not a string.
open open(content: any, options: NgbModalOptions) => NgbModalRef
Opens a new modal window with the specified content and using supplied
options. Content can be provided as a TemplateRef or a component type.
If you pass a component type as content than instances of those
components can be injected with an instance of the NgbActiveModal
class. You can use methods on the NgbActiveModal class to close /
dismiss modals from "inside" of a component.
When you reference an <ng-template>
inside a component likeViewChild('tplRef', {read: TemplateRef}) myTplRef: TemplateRef;
It creates a TemplateRef object (which you later pass to NgBootstrap modal service).
What you are trying to do on the other hand is to pass a string - which NgBootstrap does not support.
Therefore, you should pass the template reference / component as a parameter when calling your service.
Sorry - I did not notice in my comment that you open the template from the service and the template is a string.
If you look into the documentation of NgBoostrap - it opens a TemplateRef or a Component type - not a string.
open open(content: any, options: NgbModalOptions) => NgbModalRef
Opens a new modal window with the specified content and using supplied
options. Content can be provided as a TemplateRef or a component type.
If you pass a component type as content than instances of those
components can be injected with an instance of the NgbActiveModal
class. You can use methods on the NgbActiveModal class to close /
dismiss modals from "inside" of a component.
When you reference an <ng-template>
inside a component likeViewChild('tplRef', {read: TemplateRef}) myTplRef: TemplateRef;
It creates a TemplateRef object (which you later pass to NgBootstrap modal service).
What you are trying to do on the other hand is to pass a string - which NgBootstrap does not support.
Therefore, you should pass the template reference / component as a parameter when calling your service.
answered Nov 13 '18 at 15:53
Dan R.
50429
50429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263616%2fangular-7-ng-bootstrap-open-modal-from-a-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not related, but consider changing your
var
tolet
orconst
, in your template. Is a better practice.– dream88
Nov 12 '18 at 13:54
I am not so familiar with NgBootstrap but I assume you are missing some style you should be importing in your app.
– Dan R.
Nov 12 '18 at 13:59
Thanks @dream88, you are right.
– Maty
Nov 12 '18 at 14:00
@DanR. but this only happens when I open the template from this service, when I tested it on my component it worked without adding any style
– Maty
Nov 12 '18 at 14:01
@Maty try moving the css to your styles.css so the css doesnt rely on shadow dom selectors(css scoping).
– enf0rcer
Nov 12 '18 at 14:09