admin管理员组

文章数量:1406453

UPDATE: It seems Angular.js doesn't like the <ul> inside a <p>. So, as helpful menters said, replacing the <p> with a <div> solves the problem.

Sorry if this may be a newbie question on Angular.js but I just can't seem to find the error in this code. Consider the following HTML:

<div ng-app ng-controller="BlocksCtrl">
    <div ng-repeat="block in blocks">
        <div id="{{block.id}}" class="{{block.classes}}">
            <div>
                <h1>{{block.title}}, {{block.dates}}
                    <br/>
                    <small>
                        <a href="{{block.place.link}}" target="_blank">
                            {{block.place.title}}</a>
                        ({{block.place.city_country}})
                    </small>
                </h1>
            </div>
            <div>
                <div>
                    <p><i class="{{block.icon_classes}}"></i></p>
                </div>
                <div>
                    <p>
                        {{block.description.text}}
                    </p>
                    <p ng-repeat="item in block.description.items">
                        <b>{{item.title}}</b>: {{item.points.length}} - {{item.points[2].text}}
                        <ul class="fa-ul">
                            <li>
                                <i class="fa-li fa fa-check"></i>{{item.points[2].text}}
                            </li>
                            <li ng-repeat="point in item.points">
                                <i class="fa-li fa fa-check"></i>{{point.text}}
                            </li>
                        </ul>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

This is the Javascript bit:

function BlocksCtrl($scope) {
  $scope.blocks = [
    {
      id: 'my-id',
      classes: 'class1 class2',
      title: 'This is the title',
      dates: '2007 / 2011',
      place: {
        link: '/',
        title: 'This is the place',
        city_country: 'City, Country'
      },
      icon_classes: 'fa fa-terminal fa-5x',
      description: {
        text: 'description test',
        items: [
          {
            title: 'Title test',
            points: [
              {text: 'item test 1'},
              {text: 'item test 2'},
              {text: 'item test 3'},
              {text: 'item test 4'}
            ]
          }
        ]
      }
    }
  ];
}

This will display the following output (you can check a working example on JSFiddle /):


This is the title, 2007 / 2011

This is the place (City, Country)

description test

Title test: 4 - item test 3


Now can someone tell me how is it that the "{{item.points.length}} - {{item.points[2].text}}" bit works fine but the "{{item.points[2].text}}" and the ng-repeat inside the UL don't?

Thanks a bunch

UPDATE: It seems Angular.js doesn't like the <ul> inside a <p>. So, as helpful menters said, replacing the <p> with a <div> solves the problem.

Sorry if this may be a newbie question on Angular.js but I just can't seem to find the error in this code. Consider the following HTML:

<div ng-app ng-controller="BlocksCtrl">
    <div ng-repeat="block in blocks">
        <div id="{{block.id}}" class="{{block.classes}}">
            <div>
                <h1>{{block.title}}, {{block.dates}}
                    <br/>
                    <small>
                        <a href="{{block.place.link}}" target="_blank">
                            {{block.place.title}}</a>
                        ({{block.place.city_country}})
                    </small>
                </h1>
            </div>
            <div>
                <div>
                    <p><i class="{{block.icon_classes}}"></i></p>
                </div>
                <div>
                    <p>
                        {{block.description.text}}
                    </p>
                    <p ng-repeat="item in block.description.items">
                        <b>{{item.title}}</b>: {{item.points.length}} - {{item.points[2].text}}
                        <ul class="fa-ul">
                            <li>
                                <i class="fa-li fa fa-check"></i>{{item.points[2].text}}
                            </li>
                            <li ng-repeat="point in item.points">
                                <i class="fa-li fa fa-check"></i>{{point.text}}
                            </li>
                        </ul>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

This is the Javascript bit:

function BlocksCtrl($scope) {
  $scope.blocks = [
    {
      id: 'my-id',
      classes: 'class1 class2',
      title: 'This is the title',
      dates: '2007 / 2011',
      place: {
        link: 'http://www.example./',
        title: 'This is the place',
        city_country: 'City, Country'
      },
      icon_classes: 'fa fa-terminal fa-5x',
      description: {
        text: 'description test',
        items: [
          {
            title: 'Title test',
            points: [
              {text: 'item test 1'},
              {text: 'item test 2'},
              {text: 'item test 3'},
              {text: 'item test 4'}
            ]
          }
        ]
      }
    }
  ];
}

This will display the following output (you can check a working example on JSFiddle http://jsfiddle/uskL6/):


This is the title, 2007 / 2011

This is the place (City, Country)

description test

Title test: 4 - item test 3


Now can someone tell me how is it that the "{{item.points.length}} - {{item.points[2].text}}" bit works fine but the "{{item.points[2].text}}" and the ng-repeat inside the UL don't?

Thanks a bunch

Share edited Nov 14, 2013 at 11:47 A Lopes asked Nov 14, 2013 at 10:42 A LopesA Lopes 1422 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You were using <ol> tag inside <p> tag. According to Html documentation List elements (in particular, ol and ul elements) cannot be children of p elements.

So change <p ng-repeat=""> to <div ng-repeat=""> or <span ng-repeat="">

See this question in stack overflow Should ol/ul be inside <p> or outside?

At first I thought it was simple...

Then it drove me crazy...

The solution: Change the <p ng-repeat="..."> to <div ng-repeat="...">. Then it works; why, I do not know...

本文标签: javascriptngrepeat inside UL (which is inside a P) is not workingStack Overflow