angularjs模块不工作

angularjs module not working

本文关键字:工作 模块 angularjs      更新时间:2023-09-26

我正在学习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>

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

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

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

然后,在HTML文件中使用它

<html ng-app="app">

看看它在jsbin 上的工作

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

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

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

这是我正在使用的代码:

<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 }}

让我知道这里缺了什么。