对一组对象进行ng-repeat

时间:2013-06-26 17:57:03

标签: javascript angularjs

使用ng-repeat,我将如何循环以下内容:

var  messages : [
      {text:"Standard Message"},
      {text:"Success Message!", type:"success"},
      {text:"Alert Message!", type : "alert"},
      {text:"secondary message...", type : "secondary"}
    ]

我试过了:

<p ng-repeat="message in messages">{{message}}</p> 

它似乎不起作用,我该怎么做?

1 个答案:

答案 0 :(得分:35)

您需要将消息数组插入$ scope:

$scope.messages = [
      {text:"Standard Message"},
      {text:"Success Message!", type:"success"},
      {text:"Alert Message!", type : "alert"},
      {text:"secondary message...", type : "secondary"}
    ]

然后按如下方式使用它:

<p ng-repeat="message in messages">{{message.text}}</p>