如何将* ngFor的索引附加到attr.id-HTML

时间:2019-02-23 12:44:25

标签: javascript html css angular attributes

如何将索引附加到attr.id上,以便为每一行获取唯一的ID:

exports.repo_search = function(req, res, next) {
    Repo.findById(req.params.id).populate('repo').exec(function(err, repo) {
        if (err) {
            return next(err);
        }
        if (repo == null) {
            // No results.
            var err = new Error('Repo copy not found');
            err.status = 404;
            return next(err);
        }
        Dependencies.find({ Repo_id: '2' }, { dependencies: 1, _id: 0 }).exec(function(err, list_dependencies) {
            if (err) {
                return next(err);
            }
            console.log(list_dependencies);
            // Successful, so render.
            //res.render('dependencies_list', { title: 'Dependencies List', list_dependencies: list_dependencies });

            // Successful, so render.
            res.render('repo_info_detail', {
                title             : 'Repo Detail',
                repo              : repo,
                list_dependencies : list_dependencies

                //  repo_dependencies: results.repo_search
            });
            //  console.log(Repo._id);
        });
    });
};

如您所见,我正在尝试追加:

<tr *ngFor="let content of contents;let i=index;">
    <td style="width: 10%;">
      <input class="styled-checkbox" [attr.id]={{i}} + "content_item"  type="checkbox"  style="display:none;" value="" name="content-view">
      <label [attr.for]="content.id"></label>
    </td>
    <td>{{i+1}}</td>
</tr>

所以我的ID看起来像1content_item,2content_item,3content_item,等等。

但这不能正常工作,它说:

缺少属性名称..

当我尝试这样做时:

[attr.id]={{i}} + "content_item" 

它说:

  

错误:模板解析错误:

1 个答案:

答案 0 :(得分:3)

将值绑定到属性时,可以使用插值{{}}或属性绑定。由于使用的是属性绑定,因此不应使用插值

[attr.id]="i+ 'content_item'"