1
votes

I am using angular 6 to build website.Actually I want to run script on element when page is loaded.As angular ngOnit() runs script before content is loaded so I an getting problems.I found angular.element(function ()) that might solve this problem.but when I installed and imported angular into my project but I got error: Error I got is:

Error:Module not found:can't resolve'../../../node_modules/@types/angular' in 'C:\Users\name\WebProject\src\app\sales'

I also imported it to my perticular component like this:

import * as angular from '../../../node_modules/@types/angular';

My code to run script after content load is:

ngOnInit() {
       angular.element(function () {
        alert('page loading completed');
       });   }

It will be great if anyone help me out on thi or suggest me aany other way to achieve what I want. Thanks.

1
Please add your error message to the question in text form, not as an image - Nico Haase
paste your code which you have added... - Aravinthan M
angular.element() is AngularJS, Angular's predecessor, so that won't work. I don't understand yet why ngOnInit() doesn't work for you, that's the textbook place for running scripts when the element is used on the page. - J.P. ten Berge
ngOnit() does not work when I want to get attributes of any element in my layout.It just gets undefined. - Muhammad Umar
ngOnit() executes before page is loaded or rendered. - Muhammad Umar

1 Answers

0
votes

angular.element is AngularJS which is a totally different technology to Angular.

ngOnInit is too early in the component lifecycle.

Try using ngAfterViewInit instead like so:

ngAfterViewInit() {
  // code to run after view has initialised goes here
}