刚刚接触angularjs并且一直在寻找让应用程序运行的方法。非常简单直接:
角度控制器:
(function () {
'use strict';
angular
.module('myQuotesApp')
.controller('QuotesController', QuotesController);
QuotesController.$inject = ['$scope', 'Quotes'];
function QuotesController($scope, Quotes) {
$scope.quotes = Quotes.query();
}
})();
这是angularjs的服务类
(function () {
'use strict';
var QuotesService = angular.module('QuotesService', '[ngResource]');
QuotesService.factory('Quotes', ['$resource', function ($resource) {
return $resource('/api/quotes/', {}, {
query: { method: 'GET', params: {}, isArray:true }
});
}]);
})();
这是html文件:
<!DOCTYPE html>
<html ng-app="myQuotesApp">
<head>
<meta charset="utf-8" />
<title>My Quotes App</title>
<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular-resource/angular-resource.min.js"></script>
<script src="lib/angular-route/angular-route.min.js"></script>
<script src="/app.js"></script>
</head>
<body ng-cloak>
<div ng-controller="QuotesController">
<H2>List of Quotes</H2>
<ul>
<li ng-repeat="quote in quotes">
<p> "{{quote.Content}}" - {{quote.Author}}</p>
</li>
</ul>
</div>
</body>
</html>
服务器端模型:
public class Quote
{
public int Id { get; set; }
public string Author { get; set; }
public string Comment { get; set; }
}
服务器端控制器:
public IEnumerable<Quote> Get()
{
return new List<Quote> {
new Quote { Id=1, Author="James John", Comment="This guy is good to work with."},
new Quote { Id=2, Author="Larry Page", Comment="This is one of the best guys in the IT world"},
new Quote { Id=3, Author="Goodwill Adebayo", Comment="It is always good to work with God." }
};
}
每当我运行应用程序时,它都会显示一个空白页面。我检查了浏览器控制台,我收到了这个错误:
angular.js:4640Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=myQuotesApp&p1=Erro…http%3A%2F%2Flocalhost%3A60261%2Flib%2Fangular%2Fangular.min.js%3A20%3A390)
jquery-1.9.0.js:1 '//@ sourceURL' and '//@ sourceMappingURL' are deprecated, please use '//# sourceURL=' and '//# sourceMappingURL=' instead.
答案 0 :(得分:1)
你缺少一些东西。在使用之前,您需要先创建 myQuotesApp 模块。
Plunker的工作示例 - http://plnkr.co/edit/l3UmoDjjGhaQq0JNjtsF。
(function () {
'use strict';
angular
.module('myQuotesApp', ['ngResource']) // <----
.controller('QuotesController', QuotesController);
QuotesController.$inject = ['$scope', 'QuotesService'];
function QuotesController($scope, QuotesService) {
$scope.quotes = QuotesService.query();
}
angular
.module('myQuotesApp')
.factory('QuotesService', ['$resource', function ($resource) {
var query = function() {
return [{Content: "One", Author: "John"},
{Content: "Two", Author: "Eric"}]
}
return {
query: query
}
}]);
})();
您只需要更新QuotesService以使用$ resource。
(function () {
'use strict';
angular
.module('myQuotesApp', ['ngResource'])
.controller('QuotesController', QuotesController);
QuotesController.$inject = ['$scope', 'QuotesService'];
function QuotesController($scope, QuotesService) {
$scope.quotes = QuotesService.query();
console.log($scope.quotes);
}
angular
.module('myQuotesApp')
.factory('QuotesService', ['$resource', function ($resource) {
return $resource('/api/quotes/', {}, {
query: { method: 'GET', params: {}, isArray: true }
});
}]);
})();