angularjs模块无法正常工作

时间:2015-03-25 20:18:12

标签: javascript html angularjs

我正在学习AngularJS。我试图在控制器中列出变量。我在剧本中有这个。

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

demoController.controller('sampleController', function ($scope){
    $scope.customers=[
        {name:'John Smith', country:'Denmark', worth:'5000000'},
        {name:'John Lewis',country:'England',worth:'10000000'},
        {name:'Rick Evans',country:'America',worth:'6000000'}
    ];
});

我在HTML中有这个。

<html ng-app="demoController">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>

    <body>
        <h1>Applying filters</h1>
        <div ng-controller="sampleController">
            <ul>
                <li ng-repeat="cust in customers">{{ cust.name }} - {{ cust.country }} - {{ cust.worth | currency:"$":2 }}</li>
            </ul>
        </div>
    </body>
</html>

它没有列出变量。这个脚本有什么问题。谢谢!

2 个答案:

答案 0 :(得分:4)

您需要以正确的方式定义模块

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

然后,在HTML文件中使用它

<html ng-app="app">

请参阅jsbin

  

请注意,角度模块的名称是在引号

中定义的名称      

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

     

所以,如果你写了var xModule = angular.module('app2',[]);,你的模块名称是app2而不是xModule

答案 1 :(得分:0)

以下是我正在使用的代码:

<meta name="robots" content="noindex">
<html data-ng-app="app">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
<body>
<h1>Applying filters</h1>
<div data-ng-controller="sampleController">
<ul><li data-ng-repeat="cust in customers">{{ cust.name }} - {{ cust.country }} - {{ cust.worth | currency:"$":2 }}</li></ul>
</div>

<script id="jsbin-javascript">var app = angular.module('app',[]);

app.controller('sampleController', function ($scope){
    $scope.customers=[
        {name:'John Smith', country:'Denmark', worth:'5000000'},
        {name:'John Lewis',country:'England',worth:'10000000'},
        {name:'Rick Evans',country:'America',worth:'6000000'}
    ];
});
</script> 
</body></html>

输出是:

Applying filters
{{ cust.name }} - {{ cust.country }} - {{ cust.worth | currency:"$":2 }}

让我知道这里遗漏的是什么。