相关疑难解决方法(0)

如何使用ngStyle(angular2)添加背景图像?

如何使用ngStyle添加背景图像?我的代码不起作用:

this.photo = 'http://dl27.fotosklad.org.ua/20121020/6d0d7b1596285466e8bb06114a88c903.jpg';

<div [ngStyle]="{'background-image': url(' + photo + ')}"></div>
Run Code Online (Sandbox Code Playgroud)

html javascript css angular

90
推荐指数
6
解决办法
10万
查看次数

Angular 2中背景图像URL的属性属性绑定

我一直在努力找出background-image在许多Angular 2组件中动态更改属性的最佳方法.

在下面的示例中,我尝试使用指令background-image将div设置为@Input[ngStyle]:

import {Component, Input} from '@angular/core';
import { User } from '../models';

// exporting type aliases to enforce better type safety (https://github.com/ngrx/example-app)
export type UserInput = User;

@Component({
  selector: 'profile-sidenav',
  styles: [ `
    .profile-image {
      background-repeat: no-repeat;
      background-position: 50%;
      border-radius: 50%;
      width: 100px;
      height: 100px;
    }
  `],
  template: `  
    <div class="profile-image" [ngStyle]="{ 'background-image': url({{image}})">
    <h3>{{ username }}</h3>
  `
})

export class ProfileSidenav {
  @Input() user: UserInput;
  blankImage: string …
Run Code Online (Sandbox Code Playgroud)

css typescript angular-directive angular

27
推荐指数
2
解决办法
5万
查看次数

标签 统计

angular ×2

css ×2

angular-directive ×1

html ×1

javascript ×1

typescript ×1