简体   繁体   English

我不知道为什么 ng-repeat 在我的代码中不起作用

[英]I have no idea why ng-repeat is not working in my code

I am a newbie in angular and ionic and had been following a tutorial.我是 angular 和 ionic 的新手,一直在学习教程。 I tried the example code that is working just fine in the tutorial but not with me.我尝试了在教程中运行良好的示例代码,但不适用于我。 The issue is with the ng-repeat directive.问题在于 ng-repeat 指令。 Here is the code:这是代码:

<body ng-app="starter">
  <ion-pane>
    <ion-header-bar class="bar-dark">
      <h1 class="title">Artist List</h1>
    </ion-header-bar>
    <div class="bar bar-subheader item-input-inset bar-light">
      <label class="item-input-wrapper">
        <i class="icon ion-search placeholder-icon"></i>
        <input type="search" placeholder="Search">
      </label>
    </div>
    <ion-content ng-controller="ListController" class="has-subheader">
      <ion-list >
        <ion-item ng-repeat='item in artists' class="item-thumbnail-left item-text-wrap">
          <img src="http://placehold.it/100x100" alt="photo">
          <h2>{{item.name}}</h2>
          <h3>{{item.reknown}}</h3>
          <p>{{item.bio}}</p>


       </ion-list>

       </div>
      </ion-content>
    </ion-pane>
  </body>

the app.js file is: app.js 文件是:

angular.module('starter', ['ionic'])

  .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
      if(window.cordova && window.cordova.plugins.Keyboard) {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

        // Don't remove this line unless you know what you are doing. It stops the viewport
        // from snapping when text inputs are focused. Ionic handles this internally for
        // a much nicer keyboard experience.
        cordova.plugins.Keyboard.disableScroll(true);
      }
      if(window.StatusBar) {
        StatusBar.styleDefault();
      }
    });
  })
  .controller('ListController', ['$scope', '$http', function($scope, $http) {
    $http.get('js/data.json').success(function(data){
      $scope.artists = data;
    });
  }]);

the data.json file is: data.json 文件是:

[
  {
    "name":"Barot Bellingham",
    "shortname":"Barot_Bellingham",
    "reknown":"Royal Academy of Painting and Sculpture",
    "bio":"Barot has just finished his final year at The Royal Academy of Painting and Sculpture, where he excelled in glass etching paintings and portraiture. Hailed as one of the most diverse artists of his generation, Barot is equally as skilled with watercolors as he is with oils, and is just as well-balanced in different subject areas. Barot's collection entitled \"The Un-Collection\" will adorn the walls of Gilbert Hall, depicting his range of skills and sensibilities - all of them, uniquely Barot, yet undeniably different"
  },
  {
    "name":"Jonathan G. Ferrar II",
    "shortname":"Jonathan_Ferrar",
    "reknown":"Artist to Watch in 2012",
    "bio":"The Artist to Watch in 2012 by the London Review, Johnathan has already sold one of the highest priced-commissions paid to an art student, ever on record. The piece, entitled Gratitude Resort, a work in oil and mixed media, was sold for $750,000 and Jonathan donated all the proceeds to Art for Peace, an organization that provides college art scholarships for creative children in developing nations"
  },
  {
    "name":"Hillary Hewitt Goldwynn-Post",
    "shortname":"Hillary_Goldwynn",
    "reknown":"New York University",
    "bio":"Hillary is a sophomore art sculpture student at New York University, and has already won all the major international prizes for new sculptors, including the Divinity Circle, the International Sculptor's Medal, and the Academy of Paris Award. Hillary's CAC exhibit features 25 abstract watercolor paintings that contain only water images including waves, deep sea, and river."
  },
  {
    "name":"Hassum Harrod",
    "shortname":"Hassum_Harrod",
    "reknown":"Art College in New Dehli",
    "bio":"The Art College in New Dehli has sponsored Hassum on scholarship for his entire undergraduate career at the university, seeing great promise in his contemporary paintings of landscapes - that use equal parts muted and vibrant tones, and are almost a contradiction in art. Hassum will be speaking on \"The use and absence of color in modern art\" during Thursday's agenda."
  },
  {
    "name":"Jennifer Jerome",
    "shortname":"Jennifer_Jerome",
    "reknown":"New Orleans, LA",
    "bio":"A native of New Orleans, much of Jennifer's work has centered around abstract images that depict flooding and rebuilding, having grown up as a teenager in the post-flood years. Despite the sadness of devastation and lives lost, Jennifer's work also depicts the hope and togetherness of a community that has persevered. Jennifer's exhibit will be discussed during Tuesday's Water in Art theme."
  },
  {
    "name":"LaVonne L. LaRue",
    "shortname":"LaVonne_LaRue",
    "reknown":"Chicago, IL",
    "bio":"LaVonne's giant-sized paintings all around Chicago tell the story of love, nature, and conservation - themes that are central to her heart. LaVonne will share her love and skill of graffiti art on Monday's schedule, as she starts the painting of a 20-foot high wall in the Rousseau Room of Hotel Contempo in front of a standing-room only audience in Art in Unexpected Places."
  },
  {
    "name":"Constance Olivia Smith",
    "shortname":"Constance_Smith",
    "reknown":"Fullerton-Brighton-Norwell Award",
    "bio":"Constance received the Fullerton-Brighton-Norwell Award for Modern Art for her mixed-media image of a tree of life, with jewel-adorned branches depicting the arms of humanity, and precious gemstone-decorated leaves representing the spouting buds of togetherness. The daughter of a New York jeweler, Constance has been salvaging the discarded remnants of her father's jewelry-making since she was five years old, and won the New York State Fair grand prize at the age of 8 years old for a gem-adorned painting of the Manhattan Bridge."
  },
  {
    "name":"Riley Rudolph Rewington",
    "shortname":"Riley_Rewington",
    "reknown":"Roux Academy of Art, Media, and Design",
    "bio":"A first-year student at the Roux Academy of Art, Media, and Design, Riley is already changing the face of modern art at the university. Riley's exquisite abstract pieces have no intention of ever being understood, but instead beg the viewer to dream, create, pretend, and envision with their mind's eye. Riley will be speaking on the \"Art of Abstract\" during Thursday's schedule"
  },
  {
    "name":"Xhou Ta",
    "shortname":"Xhou_Ta",
    "reknown":"China International Art University",
    "bio":"A senior at the China International Art University, Xhou has become well-known for his miniature sculptures, often the size of a rice granule, that are displayed by rear projection of microscope images on canvas. Xhou will discuss the art and science behind his incredibly detailed works of art."
  }
]

You have not closed <ion-item> in your code maybe.您可能没有在代码中关闭<ion-item>

<ion-content ng-controller="ListController" class="has-subheader">
   <ion-list >
        <ion-item ng-repeat='item in artists' class="item-thumbnail-left item-text-wrap">
          <img src="http://placehold.it/100x100" alt="photo">
          <h2>{{item.name}}</h2>
          <h3>{{item.reknown}}</h3>
          <p>{{item.bio}}</p>
        </ion-item> <!--   This was missing   -->
   </ion-list>
 </div> <!--   This is extra   -->
</ion-content>

I've created a CodePen of your code and it seems everything is working perferctly.我已经为您的代码创建了一个CodePen ,看起来一切正常。

You could try adding console.log($scope.artists) after receiving the data from $http to ensure your json is received and binded in the scope.您可以尝试在收到来自$http的数据后添加console.log($scope.artists)以确保您的 json 被接收并绑定在范围内。

Hope it helps希望能帮助到你

According to your code, try to use ng-repeat in ion-list根据您的代码,尝试在ion-list使用 ng-repeat

<ion-list ng-repeat='item in artists'>
    <ion-item class="item-thumbnail-left item-text-wrap">
        <img src="http://placehold.it/100x100" alt="photo">
        <h2>{{item.name}}</h2>
        <h3>{{item.reknown}}</h3>
        <p>{{item.bio}}</p>
</ion-list>

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

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