Skip to content

Commit

Permalink
adjust example
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr committed Jun 2, 2017
1 parent a75ec0f commit 4d67a46
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions _posts/2016-01-25-learning-ng2-dynamic-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,17 @@ Assume for instance we want to dynamically add a background image of a user's pr
@Component({
selector: 'my-app',
template: `
<div [style.background-image]="getProfilePicStyle()">
<div [style]="getStyle()">
</div>
`
})
export class App {

getProfilePicStyle() {
getStyle() {
// snip snip -> fetch the url from somewhere
const profilePicUrl = 'some-remote-server-url.jpg';
return `url(${profilePicUrl}`;
const style = `background-image: url(${profilePicUrl})`;
return style;
}

}
Expand All @@ -108,12 +109,13 @@ export class App {

constructor(private sanitizer: DomSanitizer) {}

getProfilePicStyle() {
getStyle() {
// snip snip -> fetch the url from somewhere
const profilePicUrl = 'some-remote-server-url.jpg';
const style = `background-image: url(${profilePicUrl})`;

// sanitize the style expression
return this.sanitizer.bypassSecurityTrustStyle(`url(${profilePicUrl}`);
return this.sanitizer.bypassSecurityTrustStyle(style);
}

}
Expand Down

0 comments on commit 4d67a46

Please sign in to comment.