这个角度代码怎么了?

时间:2016-12-09 06:24:04

标签: javascript angularjs

我是AngularJS的新手。试图从youtube上的教程中学习。我写的是教练写的相同的代码。但不幸的是我的一个不工作。我为分享这种愚蠢的问题而道歉,但我真的很惊讶看到相同的代码对我不起作用。提前谢谢。

<body data-ng-app="">
<div data-ng-controller="SimpleController">
    <ul>
        <li data-ng-repeat="cust in Customers">
            {{cust.Name}}
        </li>
    </ul>
</div>
<script src="../Scripts/angular.js"></script>
<script>
    function SimpleController($scope) {
        $scope.Customers = [
            { Name: "Chanchal", City: "Dhaka" },
            { Name: "Masud", City: "Jessore" },
            { Name: "Humayun", City: "Barisal" }
        ];
    }
</script>

3 个答案:

答案 0 :(得分:1)

如果您使用1.3以下的角度版本,它应该可以使用,请点击此处

&#13;
&#13;
<!DOCTYPE html>
<html>

<head>
  <script  src="https://code.angularjs.org/1.2.0/angular.js"></script>
</head>

<body data-ng-app>
  <div data-ng-controller="SimpleController">
    <ul>
      <li data-ng-repeat="cust in Customers">
        {{cust.Name}}
      </li>
    </ul>
  </div>
 
<script>

function SimpleController($scope) { $scope.Customers = [ { Name: "Chanchal", City: "Dhaka" }, { Name: "Masud", City: "Jessore" }, { Name: "Humayun", City: "Barisal" } ]; }
</script>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你必须引导角度应用程序。

  

var myApp = angular.module(&#39; myApp&#39;,[]);

<script>
  var myApp = angular.module('myApp',[]);

  function SimpleController($scope) {
    $scope.Customers = [
        { Name: "Chanchal", City: "Dhaka" },
        { Name: "Masud", City: "Jessore" },
        { Name: "Humayun", City: "Barisal" }
    ];
  }
</script>

<body data-ng-app="myApp">

JSfiddle

答案 2 :(得分:0)

你不能在角度上拥有全局控制器(除非你允许全局控制器使用controllerProvider

 function SimpleController($scope) {
        $scope.Customers = [
            { Name: "Chanchal", City: "Dhaka" },
            { Name: "Masud", City: "Jessore" },
            { Name: "Humayun", City: "Barisal" }
        ];
    }

SimpleController.$inject = ['$scope'];
angular.module('myApp', []).controller('SimpleController', SimpleController);

和HTML,

<body  ng-app="myApp">