Angular 2 [style.margin-top]

时间:2016-11-12 21:55:23

标签: angular

我需要获得img的高度,然后更改margin-top从img的高度中减去。 我在下面尝试了这段代码,但遗憾的是这不起作用

<img [style.marginTop.px]="marginTop - '[style.height.px]' "class="noteImg">

这个问题多么优雅? 我很高兴你的建议。

完整代码

<div class="container-fluid noteContainer" *ngFor="let data of dataPost; let i = index">
    <div class="noteWrapperImg">
        <img [style.marginTop.px]="HOW TO DO IT?" src="{{data?.image}}" class="noteImg" alt="{{data?.altImage}}">
        <a class="{{i%2 == 1 ? 'notePDataNotEven': 'notePDataEven'}}" [class.css]="i%2 == 1">
           <i class="ion-ios-clock-outline"></i> {{data?.data}}
        </a>
    </div>
</div>

2 个答案:

答案 0 :(得分:11)

正确节点的样式margin-top属性为marginTop,但你不能这样做。您应该首先访问组件中的样式并检索height属性,然后在模板中使用它。

当您使用*ngFor循环时,可以通过向图像添加模板变量并将其传递给返回其高度的某个方法来实现。像这样:

<div *ngFor="let data of dataPost>
    <img #img [style.marginTop.px]="getImgMarginTop(img)" src="http://image-src.png">
</div>

然后向组件添加一个返回图像高度的方法。

  getImgMarginTop(img) {
    return img.style.marginTop - img.clientHeight
  }

答案 1 :(得分:2)

据我所知,您无法通过使用指令#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { FILE *fp; int max_x, max_y, num_pt; int *x_coordinate, *y_coordinate; int inputfile = 0, outputfile = 0; int i; int count,dist,spot; char black[24],white[24]; typedef struct{ char colour[24]; int x; int y; int pos; }table; strcpy(black,"black"); strcpy(white,"white"); if (argc == 1) { /* to generate random instances, accepting parameters from stdin */ return 1; } for (i = 1; i < argc; i++) { if (strcmp (argv[i], "-i") == 0) inputfile = i+1; else if (strcmp (argv[i], "-o") == 0) outputfile = i+1; } if (inputfile == 0) { printf("Error File does not exit!\n"); return -1; } if ((fp = fopen(argv[inputfile], "r")) == NULL) { printf("Error name of file does not exist!\n"); return -2; } while (fscanf(fp, "%d", &max_x) != 1) { if (ferror(fp)) { /* read error */ printf("Error max value does not equal 1!\n"); fclose(fp); return -3; } if (feof(fp)) { /* no integer to read */ printf("Error no numbers to read in file!\n"); fclose(fp); return -4; } fscanf(fp, "%*[^\n]"); /* skip the rest of line */ } if (fscanf(fp, "%d", &max_y) != 1) { /* max_y not following max_x */ printf("Error format for x not follwing y\n"); fclose(fp); return -5; } while (fscanf(fp, "%d", &num_pt) != 1) { if (ferror(fp)) { /* read error */ printf("Error num_pt != 1\n"); fclose(fp); return -6; } if (feof(fp)) { /* no integer to read */ printf("Error file is empty!\n"); fclose(fp); return -7; } fscanf(fp, "%*[^\n]"); /* skip the rest of line */ } x_coordinate = (int *)malloc(num_pt * sizeof(int)); y_coordinate = (int *)malloc(num_pt * sizeof(int)); for (i = 0; i < num_pt; i++) { while (fscanf(fp, "%d", &x_coordinate[i]) != 1) { if (ferror(fp)) { printf("Coordinate reading error!\n"); /* read error */ fclose(fp); return -8; } if (feof(fp)) { /* no integer to read */ printf("Error no integers to read\n"); fclose(fp); return -9; } fscanf(fp, "%*[^\n]"); /* skip the rest of line */ } if (fscanf(fp, "%d", &y_coordinate[i]) != 1) { /* y_coordinate not following x_coordinate */ printf("Error x/y not following each other\n"); fclose(fp); return -10; } } fclose(fp); for(count=0;count<num_pt;count++){ strcpy(nodes[count].colour,white); nodes[count].pos = count; nodes[count].x = x_coordinate[count]; nodes[count].y = y_coordinate[count]; } table nodes[num_pt],parent[1],small[num_pt],mst[num_pt]; mst[0] = nodes[0]; strcpy(nodes[0].colour,black); parent[0] = nodes[0]; for(spot=1;spot<num_pt;spot++){ for(count=0;count<num_pt;count++){ dist = (nodes[count].x-parent[0].x)+(nodes[count].y-parent[0].y); if (dist > 0 && strcmp(nodes[count].colour,white) == 0){ small[0] = nodes[count]; } } printf("Found node %d --> nodes %d to be closest\n",parent[0].pos,small[0].pos); parent[0] = small[0]; mst[spot] = small[0]; strcpy(nodes[small[0].pos].colour,black); } //int labelx=0,labely=0; /* if (outputfile > 0) { if ((fp = fopen(argv[outputfile], "w")) == NULL) { printf("Error, can't open print file\n"); return -2; } fprintf(fp, "##################################################\n"); fprintf(fp, "#%s\n", argv[inputfile]); fprintf(fp, "#area [0, MAX_X] x [0, MAX_Y]\n"); fprintf(fp, "%d\t%d\n", max_x, max_y); fprintf(fp, "#number of points NUM_PT\n"); fprintf(fp, "%d\n", num_pt); fprintf(fp, "#coordinates\n"); for (i = 0; i < num_pt; i++) { fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]); } fprintf(fp, "#end of instance\n"); fprintf(fp, "#Edges of the MST by Prim's algorithm:\n"); fclose(fp); } */ printf("Choosing point 0 as root ...\n"); for (i = 1; i < num_pt; i++) { printf("Point %d has a distance %d to the tree, parent 0;\n", i,(abs(x_coordinate[0] - x_coordinate[i])) + (abs(y_coordinate[0] - y_coordinate[i]))); } printf("#Edges of the MST by Prim's Algorithm\n"); /* for(count=0;count<num_pt;count++){ totalmst=totalmst+mst[count].dist; printf("%d %d %d\n",mst[count].coord[1],mst[count].coord[0],mst[count].dist); } printf("The total length of the MST is %d.\n",totalmst); */ free(x_coordinate); free(y_coordinate); return 0; } 获得价值,您最好的选择是使用ElementRef直接访问DOM,然后访问其height属性,在计算中使用它