简体   繁体   English

显示链或消息的角度指令

[英]Angular directive to show chain or messages

There is the following HTML markup: 有以下HTML标记:

<section class="correspondence">
  <header>
    <div class="from">{{ message.from }}</div>
    <div class="when">{{ message.when }}</div>
  </header>
  <div class="content">
    {{ message.content }}
    <section class="correspondence">
    ...
    </section>
  </div>
</section>

As you can see this is a markup has nested repeat content (chain of letters) - I'd like to print array of messages $scope.messages. 如您所见,这是一个嵌套了重复内容(字母链)的标记-我想打印消息数组$ scope.messages。 How can I do it? 我该怎么做? I think I should create some directive for it. 我想我应该为此创建一些指令。 Can you help me? 你能帮助我吗? Thanks! 谢谢!

Try this........ u need to use ng repeat 试试这个........你需要使用ng repeat

HTML code same.and add the following script code. HTML代码相同。并添加以下脚本代码。

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.messages = [{
        from: "test_from_1",
        when: "test_when_1",
        content: "test_content_1"
    }, {
        from: "test_from_2",
        when: "test_when_2",
        content: "test_content_2"
    }];

});

this is a working example : http://plnkr.co/edit/dZ473fpX9BrxjPH3Cb3L?p=preview 这是一个可行的示例: http : //plnkr.co/edit/dZ473fpX9BrxjPH3Cb3L?p=preview

in the controller: 在控制器中:

$scope.messages = [{
    from: "from_1",
    when: "when_1",
    content: "content_1"
}, {
    from: "from_2",
    when: "when_2",
    content: "content_2"
},{
    from: "from_3",
    when: "when_3",
    content: "content_3"
}];

in the html, use the ng-repeat to loop on a array ( this will create as many section as messages ) : 在html中,使用ng-repeat在数组上循环(这将创建与messages一样多的部分):

<section ng-repeat="message in messages" class="correspondence">
  <header>
    <div class="from">{{ message.from }}</div>
    <div class="when">{{ message.when }}</div>
  </header>
  <div class="content">
    {{ message.content }}
    <section class="correspondence">
    ...
    </section>
  </div>
</section>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM