首页 > 解决方案 > changing element in ngOnInit not working when changing route, but is able to access element

问题描述

I have an angular project with a router. In the ngOnInits for the components I want to dynamically set a a p tags value, and an img tags href. It works on inital load, but when i change routes the values dont change. The ngOnInit function is called, but the editing of the two elements does not work.

document.getElementById('discord-tag').innerHTML = username;
document.getElementById('profilePicture').setAttribute('src', image_url)

my code is simple.

i know the ngOnInit function is working because i put some console.log statements in there to test and they run each time i click on a route link.

标签: javascriptangulartypescript

解决方案


Try ngAfterViewInit() for changing 2 elements.


推荐阅读