最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Increment [ngStyle] attr using ngFor index? - Stack Overflow

programmeradmin2浏览0评论

I'm trying to dynamically create several elements using ngFor and then set the top attribute depending on the amount drawn.

I'm wondering if there is a way to access the index of ngFor on the same div for ngStyle? i.e;

<div class="row" *ngFor="let d of data; let i = index;" [ngStyle]="{'top': mrTop*i}" >

If not, any suggestions how I can achieve something similar?

I would prefer to avoid adding another div like;

<div *ngFor="let d of data; let i = index;">
  <div class="testCase" [ngStyle]="{'top': mrTop*i}">{{d}}</div>
</div>

(Although this doesn't work either)

I am wondering if there is a way to attach an event listener to the loop event so that behind the scenes I can increment the mrTop variable for each div drawn?

Anyway, I'm not sure how best to approach this and hoping for some help/advice.

Plunk here

I'm trying to dynamically create several elements using ngFor and then set the top attribute depending on the amount drawn.

I'm wondering if there is a way to access the index of ngFor on the same div for ngStyle? i.e;

<div class="row" *ngFor="let d of data; let i = index;" [ngStyle]="{'top': mrTop*i}" >

If not, any suggestions how I can achieve something similar?

I would prefer to avoid adding another div like;

<div *ngFor="let d of data; let i = index;">
  <div class="testCase" [ngStyle]="{'top': mrTop*i}">{{d}}</div>
</div>

(Although this doesn't work either)

I am wondering if there is a way to attach an event listener to the loop event so that behind the scenes I can increment the mrTop variable for each div drawn?

Anyway, I'm not sure how best to approach this and hoping for some help/advice.

Plunk here

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Dec 20, 2016 at 8:02 alexcalexc 1,3102 gold badges18 silver badges46 bronze badges 3
  • Have you tried it? I don't see why it wouldn't work. – Günter Zöchbauer Commented Dec 20, 2016 at 8:06
  • @GünterZöchbauer I have, but it doesn't seem to be working. Unless I've missed something - I included a plunk as an example which demonstrates the test case – alexc Commented Dec 20, 2016 at 8:08
  • I missed that link (as always if they are not emphasized) |-/ – Günter Zöchbauer Commented Dec 20, 2016 at 8:10
Add a ment  | 

2 Answers 2

Reset to default 6

Your mrTop variable is a string, you can't multiply it.

Try:

public mrTop = 10;

and then

<div [ngStyle]="{'top': mrTop*i + '%'}">

or

<div [style.top.%]="mrTop*i">

There are several mistakes

'10%' * i // not valid number

public mrTop: 10; // defines mrTop as of type 10 which doesn't make sense
// it should be public mrTop= 10;

Ng style can look like

[ngStyle]="{'top': mrTop * i + '%'}"

Plunker example

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>