为什么我的 ng-repeat 不起作用 (angularJs)
Why doesn't my ng-repeat work(angularJs)
我使用 angularjs(1.6)。我在使用 ng-repeat 时遇到了一些麻烦。我的 constructor
中有一个 object 数组(带有问题、标题...)。根据今天的日期,我使用一个变量从数组中得到一个 object。这个object我想用ng-repeat来显示,但是我想一定是我弄错了。没有任何反应。
这是构造函数中 object 的示例:
this.questions = [
{
date: '20/3',
question: `How odl are you ?`,
answer: { r1: '12', r2: '28', r3: '32' },
},
{
date: '21/3',
question: `What's your name? ?`,
answer: { r1: 'Math', r2: 'Eric', r3: 'Ben' },
},....
]
returns object 的变量,我想按天显示的值:
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
const today = day + "/" + month;
this.newDates = this.questions.find(q => q.date === today)
我想显示 newDates
object 包含的内容。
这里是 html:
<h1 ng-repeat="newDate in ::$ctrl.newDates">{{ ::newDate.question }}</h1>
我一定是犯了一个愚蠢的错误,或者我无法显示动态创建的数据object...如果您能帮助我,我找不到解决方案。
您需要使用Array.filter()
which return an Array where as Array.find()
对象
this.newDates = this.questions.filter(q => q.date === today)
注意:如果您希望单个对象保留现有代码并且不使用 ngRepeat
直接使用
<h1>{{ ::$ctrl.newDates.question }}</h1>
this.newDates = this.questions.filter(q => q.date === today)
我使用 angularjs(1.6)。我在使用 ng-repeat 时遇到了一些麻烦。我的 constructor
中有一个 object 数组(带有问题、标题...)。根据今天的日期,我使用一个变量从数组中得到一个 object。这个object我想用ng-repeat来显示,但是我想一定是我弄错了。没有任何反应。
这是构造函数中 object 的示例:
this.questions = [
{
date: '20/3',
question: `How odl are you ?`,
answer: { r1: '12', r2: '28', r3: '32' },
},
{
date: '21/3',
question: `What's your name? ?`,
answer: { r1: 'Math', r2: 'Eric', r3: 'Ben' },
},....
]
returns object 的变量,我想按天显示的值:
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1;
var day = dateObj.getUTCDate();
const today = day + "/" + month;
this.newDates = this.questions.find(q => q.date === today)
我想显示 newDates
object 包含的内容。
这里是 html:
<h1 ng-repeat="newDate in ::$ctrl.newDates">{{ ::newDate.question }}</h1>
我一定是犯了一个愚蠢的错误,或者我无法显示动态创建的数据object...如果您能帮助我,我找不到解决方案。
您需要使用Array.filter()
which return an Array where as Array.find()
对象
this.newDates = this.questions.filter(q => q.date === today)
注意:如果您希望单个对象保留现有代码并且不使用 ngRepeat
直接使用
<h1>{{ ::$ctrl.newDates.question }}</h1>
this.newDates = this.questions.filter(q => q.date === today)