0

I'd appreciate if someone could advise on my problem with Angular data binding in a component.

I have the app that loads a component dynamically into some modal using iframe. The user can open several instances of a single page, for example fill the form with data A in modal A and fill the same form with data B in modal B, so that two modals appear on the page at the same time. This is the same as if I opened single page in several browser tabs.

The problem is when I change some ngModel in one form, it automatically changes in another, so when I enter data into the form, all opened forms receive the same values.

My root component:

index.html

<html>
...
    <body>
      <kb-root>
      </kb-root>
    </body>
</html>

root.component.ts

@Component({
    selector: "kb-root",
    template: "<router-outlet></router-outlet>"
})

export class rootComponent {}

app.module.ts

const appRoutes: Routes = [
    { path: '', component: appComponent },
}
@NgModule({
imports: [ RouterModule.forRoot(appRoutes)] })

app.component.ts

@Component({
    selector: "kb-app",
    templateUrl: "..",
})
export class appComponent{}

app.desktop.ts

export class appDesktop{
    parent: appComponent = null;
     constructor(private inj: Injector) {
        this.parent = this.inj.get(appComponent);
    }
    //fires on link click, where route is the href of the link
    openApp(route: string) {
        $("kb-app").append('<div id="' + target + '"><iframe src="/' + route+ '"></div>');
    }
}

I'm new to Angular and would be grateful for any suggestions or references on preventing this behaviour.

Gyuzal
  • 1,581
  • 10
  • 52
  • 99
  • If you're loading them on the same page (in the same app) then it's the same instance which means they share `ngModel` – anteAdamovic Nov 08 '17 at 12:21
  • Can you update your question with the code? – Yoni Affuta Nov 08 '17 at 12:24
  • Exactly, you have the same instance of your component class. It is not "as if I opened single page in several browser tabs", not at all, because then you would have separate instances of your whole app. Maybe start here: https://stackoverflow.com/questions/38482357/angular2-how-to-use-multiple-instances-of-same-service – Vali Munteanu Nov 08 '17 at 12:26
  • Please see my edits, in my case even if I open page in several tabs, i still have the same instance. – Gyuzal Nov 09 '17 at 05:03
  • @ValiMunteanu, maybe the reason in `iframe`? – Gyuzal Nov 09 '17 at 06:40

0 Answers0