AngularJS控制器显示错误

时间:2016-08-22 11:07:52

标签: angularjs-scope

我是AngularJS的新手并坚持输出。

以下是代码段:

<html ng-app>  
<body>  
<div class="container" data-ng-controller="simpleController">  
    <ul>
        <li ng-repeat="cust in customers | orderBy:'name'">
        {{ cust.name | uppercase }} - {{ cust.city | lowercase }} - {{                   cust.balc | currency }}
        </li>
    </ul>
</div>  
<script>
    angular.module("app",[]).controller("simpleController",function($scope){  
        $scope.customers = [  
                           { name: 'Dave', city: 'Jaipur', balc: '500'},  
                           { name: 'Shruti', city: 'Toronto', balc: '1000'},  
                           { name: 'Rishu', city: 'Phoenix', balc: '2000'},  
                           { name: 'Shweta', city: 'California',balc:'3000'}
                           ];  
    }  
</script>
</body>
</html>

有人能告诉我我的代码有什么问题。 我在div标签和li标签之前放了单引号,因为它没有在stackoverflow上显示。

2 个答案:

答案 0 :(得分:0)

在ng-app中,您没有传递模块名称。 请找到我用代码更新它的JSFiddle链接。

HTML Code
<html ng-app="app">

  <body>
    <div class="container" data-ng-controller="simpleController">
      <ul>
        <li ng-repeat="cust in customers | orderBy:'name'">
          {{ cust.name | uppercase }} - {{ cust.city | lowercase }} - {{ cust.balc | currency }}
        </li>
      </ul>
    </div>

  </body>

</html>

JS Code
angular.module("app", []).controller("simpleController", function($scope) {
  $scope.customers = [{
    name: 'Dave',
    city: 'Jaipur',
    balc: '500'
  }, {
    name: 'Shruti',
    city: 'Toronto',
    balc: '1000'
  }, {
    name: 'Rishu',
    city: 'Phoenix',
    balc: '2000'
  }, {
    name: 'Shweta',
    city: 'California',
    balc: '3000'
  }];
})

http://jsfiddle.net/rs64h0jz/

答案 1 :(得分:0)

上面的答案是正确的,但我想你想在ng-repeat中使用它们,所以你可以在表达式中使用你想要的任何名称,这个名称将是你数组的当前元素。你必须像这样包装它:

`<div ng-repeat="cust in customers">{{ cust.name | uppercase }} - {{ cust.city | lowercase }} - {{ cust.balc | currency }}</div>`