Javascript Angularjs中的模块问题
我有以下服务,它使用一个叫做新闻工厂的工厂。在这个工厂里,我有一个json数组 services.jsJavascript Angularjs中的模块问题,javascript,angularjs,json,Javascript,Angularjs,Json,我有以下服务,它使用一个叫做新闻工厂的工厂。在这个工厂里,我有一个json数组 services.js 'use strict'; angular.module('larissaApp') .factory('newsFactory',function(){ var newsfac = {}; var news = [
'use strict';
angular.module('larissaApp')
.factory('newsFactory',function(){
var newsfac = {};
var news = [
{
_id: 0,
title: 'Camping in the City and this year',
image:'img/news1',
label: '',
description: '...',
comments: ''
},
{
_id: 1,
title: 'International Museum Day 18 May 2017',
image:'img/news2',
label: '',
description: '... ',
comments: ''
}
]
newsfac.getNews = function(){
return news;
}
newsfac.getNewsIndex = function(index){
return news[index];
}
return newsfac;
});
'use strict';
angular.module('larissaApp')
.controller('IndexController',
['$scope','newsFactory',function($scope,newsFactory){
$scope.news = newsFactory.getNews();
}]);
接下来,我声明了一个名为IndexController的控制器。在这里,我调用在工厂中定义的getNews()函数,该函数是在services.js中创建的
controllers.js
'use strict';
angular.module('larissaApp')
.factory('newsFactory',function(){
var newsfac = {};
var news = [
{
_id: 0,
title: 'Camping in the City and this year',
image:'img/news1',
label: '',
description: '...',
comments: ''
},
{
_id: 1,
title: 'International Museum Day 18 May 2017',
image:'img/news2',
label: '',
description: '... ',
comments: ''
}
]
newsfac.getNews = function(){
return news;
}
newsfac.getNewsIndex = function(index){
return news[index];
}
return newsfac;
});
'use strict';
angular.module('larissaApp')
.controller('IndexController',
['$scope','newsFactory',function($scope,newsFactory){
$scope.news = newsFactory.getNews();
}]);
最后,我想将json数组的所有对象显示为一个列表。因此,我使用下面的一段代码
index.html
<div class="row row-content" ng-controller="IndexController">
<div class="col-xs-12 col-sm-9">
<h2>News</h2>
<ul class="media-list">
<li class="media" ng-repeat="newsArticle in news">
<div class="media-left media-middle">
<a>
<img class="media-object img-thumbnail"
ng-src={{newsArticle.image}} alt="Uthappizza">
</a>
</div>
<div class="media-body">
<h2 class="media-heading">{{newsArticle.title}}</h2>
</div>
</li>
</ul>
</div>
<div class="col-xs-12 col-sm-3">
<p style="padding:20px;"></p>
</div>
</div>
从我的研究中,我发现模块名没有加载或拼写错误。我不明白。services.js和contollers.js中的我的模块都定义为larissaApp。另外,我正在导入这些文件
<script src="../bower_components/angular/angular.min.js"></script>
<script src="scripts/services.js"></script>
<script src="scripts/controllers.js"></script>
您还必须使用ng-app以HTML格式加载您的larissaApp模块,您可以按如下操作:
您还必须使用ng-app以HTML格式加载您的larissaApp模块,您可以按如下操作:
您需要纠正以下事项:
ng app=“larissaApp”
,如果尚未定义empty dependency
array来更正模块声明。比如:angular.module('larissaApp',[])
这是第一次请参见此操作您需要纠正以下事项:
ng app=“larissaApp”
,如果尚未定义empty dependency
array来更正模块声明。比如:angular.module('larissaApp',[])
这是第一次在给定的代码片段中,我看不到您在任何地方定义了
larissaApp
,我可以看到您正在尝试使用它。你需要定义
angular.module('larissaApp',[])
可以在单独的模块中,也可以在控制器或服务中。注意[]
如果没有函数,这是定义模块和角度的一种方式。模块('larissaApp')是使用模块的一种方式。您可以在[]下插入模块依赖项。在给定的代码段中,我看不到您在任何地方定义了larissaApp
,我可以看到您正在尝试使用它。你需要定义
angular.module('larissaApp',[])
可以在单独的模块中,也可以在控制器或服务中。注意[]
如果没有函数,这是定义模块和角度的一种方式。模块('larissaApp')是使用模块的一种方式。您可以在[]下插入模块依赖项。angular.module('larissaApp')
是现有模块的获取程序,因此如果这是您第一次声明它,请将其更改为angular.module('larissaApp',[])
(然后在下次需要将内容绑定到它时使用angular.module('larissaApp')
)angular.module('larissaApp')
是现有模块的一个getter,因此如果这是您第一次声明它,请将其更改为angular.module('larissaApp',[])
(然后在下次需要绑定时使用angular.module('larissaApp')
)乐意帮助您:)乐意帮助您:)