ng-repeat不输出结果

时间:2016-02-03 01:08:30

标签: javascript angularjs

我在学习上还是新手,虽然我说得对,但我无法在第一个应用中输出成果。 我在body.fruits"中使用ng-repeat ="食物。输出,但没有任何显示。有什么想法吗?

<!DOCTYPE html>
<html ng-app="app">
  <head>
    <title>Bootstrap 3</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script type="text/javascript" src="angular.min.js"></script>
            <script type="text/javascript" src="app.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link href="index.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="css/reset.css">
  </head>

  <body ng-controller="bodyController as body">
    <div class="navbar navbar-inverse navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a href="#" class="navbar-brand">Tech Site</a>
        </div>
        <div class="collapse navbar-collapse navHeaderCollapse">
          <ul class="nav navbar-nav">
            <li><a href="#">Specs and Features</a></li>
            <li><a href="#">How To Videos</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">Social Media <b class = "caret"></b></a>
              <ul class="dropdown-menu ">
                <li><a href="#">Twitter</a></li>
                <li><a href="#">Facebook</a></li>
                <li><a href="#">Google+</a></li>
                <li><a href="#">Instagram</a></li>
              </ul>
            </li>
            <li></li>
          </ul>
        </div>
      </div>
    </div>

    <div class="container">      
      <div class="row">
        <div class="col-md-2 " ></div>
        <div class="col-md-8 " >
        <div id="appleFacts" ng-repeat= "foods in body.fruits"></div>

        </div> 
        <div class="col-md-2 " ></div>  
      </div>
</div>

    <script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
  </body>
</html>

App.js

(function() {

    var app = angular.module('app', []);
    app.controller("bodyController", function(){
        this.foods = fruits;
        });
    var fruits = [
        { name: 'Apples', calories: 65, totalFat: '0g', Saturated_Fat: '0g', Cholestrol: '0mg', 
          sodium: '1mg', totalCarbs: '17g', dietaryFiber: '3g', Sugar: '13g', 
          Protein: '0g', vitaminA: '1%', vitaminC: '10%', Calcium: '1%', Iron: '1%'
        },
        { name: 'Oranges', price: 5.95 },
        { name: 'Pineapple', price: 3.95 }
    ];
    })();

2 个答案:

答案 0 :(得分:0)

您需要定义控制器并将其包含在角度应用模块中。

(function() {

  var app = angular.module('app', []);

  var BodyController = function() {
    var vm = this;

    vm.fruits = [
      {name: 'Apples',price:1.00},
      {name: 'Oranges',price:2.00},
      {name: 'Pineapples',price:3.00},
      ];
  };

  app.controller('BodyController', BodyController);
})();

完成后,您需要在ng-repeat中模拟转发器逻辑:

<body ng-controller="BodyController as vm">

...

<div id="appleFacts" ng-repeat="food in vm.fruits">
  <span>{{food.name}}</span>
</div>

请参阅此处的工作示例:http://plnkr.co/edit/TDh0Oes4KGeMrC7vNq3h?p=preview

答案 1 :(得分:0)

在App.js文件中,您需要使用 $ scope 模块。这是您从html文件中的js文件访问属性的方法。所以它看起来像:

app.controller("bodyController", function($scope){ $scope.foods = fruits;

然后在html:

<div id="appleFacts" ng-repeat= "food in foods"> {{food.name}} </div>

您可以使用上述语法访问这些对象中的任何其他属性:{{food.calories}}或{{food.price}}等。