Javascript AngularJS未将对象属性绑定到模板/html元素
对于angularJS来说,这是一个相当新的话题,我正在看Todd的付费教程,我不知道为什么,但是我的对象属性不会出现在页面上Javascript AngularJS未将对象属性绑定到模板/html元素,javascript,html,angularjs,Javascript,Html,Angularjs,对于angularJS来说,这是一个相当新的话题,我正在看Todd的付费教程,我不知道为什么,但是我的对象属性不会出现在页面上 function AddCurrentJobs($scope){ $scope.jobinfo = [{ title: 'Building Shed', description: 'I need helping building a shed' }, { title: 'Jello', description: 'Test 123' }]
function AddCurrentJobs($scope){
$scope.jobinfo = [{
title: 'Building Shed',
description: 'I need helping building a shed'
},
{
title: 'Jello',
description: 'Test 123'
}];
}
angular
.module("app")
.controller("AddCurrentJobs", AddCurrentJobs);
HTML
{{jobinfo}}
div元素中的结果:[{“title”:“Building Shed”,“description”:“我需要帮助构建Shed”},{“title”:“Jello”,“description”:“Test 123”}]
现在,如果将其更改为jobinfo.title,则返回时元素中没有任何内容。
我尝试使用repeat指令,但也不起作用。
控制台中没有错误。
我做错了什么?您试图访问HTML中类似数组的对象,您可以使用其索引访问特定元素
<div ng-controller="AddCurrentJobs">
<h1>{{jobinfo[0].title}}</h1>
</div>
{{jobinfo[0].title}
要添加到Sajeetharan的答案中,您可以使用显示作业信息列表中每个对象的数据ng repeat
类似于foreach
循环,因此语法为“[list]中的[object]”,然后您可以像平常一样访问该对象上的属性
<div ng-controller="AddCurrentJobs">
<div ng-repeat="job in jobinfo">
<h1>{{job.title}}</h1>
</div>
</div>
{{job.title}
jobinfo[0].title(它是一个保存对象的数组)您的重复尝试看起来是否像{{job.title}
?@LukeGaskell是的,正如Sajeetharan所说,但我的ng repeat也做了同样的事情{jobinfo[0].title}}{{job.jobinfo}
@taki它看起来像是我在yoursSorry下发布的评论,我应该提出关于使用ng repeat的问题,但这确实解决了在数组中选择特定值的问题。是的,如果它的数组你应该使用ng repeat。马克:这是否有用
<div ng-controller="AddCurrentJobs">
<div ng-repeat="job in jobinfo">
<h1>{{job.title}}</h1>
</div>
</div>