gpt4 book ai didi

angularjs - 如何在不使用 ng-repeat 的情况下显示单个对象数据?

转载 作者:行者123 更新时间:2023-12-03 07:01:07 25 4
gpt4 key购买 nike

目标:- 仅显示单个对象数据。我必须使用 ng-repeat 来获取对象吗?

我对 Angular 还比较陌生。有更好的方法吗?

Html View :-

<div ng-controller="dashboardController">
<div ng-repeat="person in persons">
<span class="name">{{person.name}}</span>
<span class="title">{{person.title}}</span>
</div>
</div>

Controller 代码:-

.controller('dashboardController', ['$scope', function(scope){
scope.persons = [
{
name:"George Harrison",
title:"Goof Off extraordinaire"
}
];
}])

更新我的菜鸟同胞,数组与单个数据集:

scope.persons = [ <-- that creates an array. Cant believe I forgot that.
scope.persons = { <-- that creates a single data set

最佳答案

scope.persons 是一个数组,所以你必须使用 ng-repeat。如果你的数据是一个对象,则不需要使用 ng-repeat。前任:你的 Controller

    .controller('dashboardController', ['$scope', function(scope){
scope.person ={
name:"George Harrison",
title:"Goof Off extraordinaire"
}

}]);

所以你的html:

<div ng-controller="dashboardController">
<div>
<span class="name">{{person.name}}</span>
<span class="title">{{person.title}}</span>
</div>

关于angularjs - 如何在不使用 ng-repeat 的情况下显示单个对象数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35779786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com