The company I am working for have decided to upgrade to Angular2 although they do not wish to use typescript for various reasons.
We are updating data that Angular is managing outside of the Angular scope and therefore would like to access functions defined within the Class function's object literal outside of Angular.
Unfortunately, when attempting to access the function defined below as updateAngularData outside of Angular the function is undefined along with all members of the Component object and I am unsure as to why.
(function(app)
{
let scripts = document.getElementsByTagName("script")
let current_script_path = scripts[scripts.length-1].src;
var core_1 = ng.core;
app.AppComponent = ng.core.Component(
{
selector: 'angular-template',
templateUrl: current_script_path.replace('script.js', angular_2_template_path)
}).Class(
{
constructor: function()
{
this.model = data_store;
this.ui_handler = ui_handler;
this.ui_handler.model = this.model;
this.zone = core_1.NgZone;
},
updateAngularData : function(data)
{
this.zone.run( function () { this.model.data = data } );
},
ngAfterViewInit : function ()
{
on_angular_load_callback();
this.updateAngularData(undefined); //This works
}
}
);
})
(this.app);
(function(app)
{
app.AppModule = ng.core.NgModule(
{
imports: [ ng.platformBrowser.BrowserModule ],
declarations: [ app.AppComponent ],
bootstrap: [ app.AppComponent ]
}).Class(
{
constructor: function()
{
},
});
})
(this.app);
(function(app)
{
document.addEventListener('DOMContentLoaded', function()
{
ng.platformBrowserDynamic.platformBrowserDynamic().bootstrapModule(app.AppModule);
});
})
(this.app);
The code then to access the updateAngularData outside of angular is then accessed from a callback in a completely different script:
this.app.AppComponent.updateAngularData(e); //updateAngularData is now undefined
I am sure I am missing something here although I can't help but think that the instance of the class created by Angular is stored within ng.core and therefore any attempt to access any of the functions defined within it are undefined. Is this right? If so, how could I go about fixing this?