使用 AngularJS 中的服务
Use Services in AngularJS
我正在尝试使用 angularjs 发出 REST 请求。
Main.js
angular.module('frontendApp')
.controller('MainCtrl', ['$scope', '$http', function($scope, $http)
{
$scope.loadData = function() {
$scope.services = Services.query();
};
services.js
'use strict';
angular.module('frontendApp').factory('Services', function($resource) {
return $resource('/services/:serviceId', {
serviceId: '@_id'
}, {});
});
我收到错误消息:未定义服务。我做错了什么?
编辑:
Mains.html
<div class="jumbotron">
Choose A Category1
<div class="wrap" ng-controller="MainCtrl">
<select ng-model="selectedValue" ng-change="loadData()" >
<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
</select>
<div ng-show="selectedValue != null" class="main">
<center><h2>Results For Category {{ selectedValue }}</h2></center> <br><br>
</div>
<div class="main" >
<ul class="cloudlist">
<li class="service" ng-repeat="item in services" ng-click="select(item)">
<div class="info">
<h3>{{item.service_name}}</h3>
<b>{{item.status_page}}</b><br>
<b>Is Billed : {{item.is_billed.billing_term._identifier}}</b>
</div>
</li>
</ul>
<br>
</div>
</div>
在控制器中注入服务后
angular.module('frontendApp')
.controller('MainCtrl', ['$scope', '$http', 'Services', function ($scope, $http, Services) {
$scope.loadData = function () {
$scope.services = Services.query();
};
我收到这个错误:
未知提供者:ServicesProvider <- Services <- MainCtrl
您必须在您的控制器中注入服务。
var module = angular.module('frontendApp', ['ngResource']);
module.factory('MyServices', ['$resource', function($resource) {
return $resource('/services/:serviceId', {
serviceId: '@_id'
}, {});
}]);
module.controller('MainCtrl', ['$scope', '$http','MyServices', function ($scope, $http, MyServices) {
$scope.loadData = function () {
$scope.services = MyServices.query();
};
}]);
我正在尝试使用 angularjs 发出 REST 请求。
Main.js
angular.module('frontendApp')
.controller('MainCtrl', ['$scope', '$http', function($scope, $http)
{
$scope.loadData = function() {
$scope.services = Services.query();
};
services.js
'use strict';
angular.module('frontendApp').factory('Services', function($resource) {
return $resource('/services/:serviceId', {
serviceId: '@_id'
}, {});
});
我收到错误消息:未定义服务。我做错了什么?
编辑: Mains.html
<div class="jumbotron">
Choose A Category1
<div class="wrap" ng-controller="MainCtrl">
<select ng-model="selectedValue" ng-change="loadData()" >
<option value="1">Category 1</option>
<option value="2">Category 2</option>
<option value="3">Category 3</option>
<option value="4">Category 4</option>
<option value="5">Category 5</option>
</select>
<div ng-show="selectedValue != null" class="main">
<center><h2>Results For Category {{ selectedValue }}</h2></center> <br><br>
</div>
<div class="main" >
<ul class="cloudlist">
<li class="service" ng-repeat="item in services" ng-click="select(item)">
<div class="info">
<h3>{{item.service_name}}</h3>
<b>{{item.status_page}}</b><br>
<b>Is Billed : {{item.is_billed.billing_term._identifier}}</b>
</div>
</li>
</ul>
<br>
</div>
</div>
在控制器中注入服务后
angular.module('frontendApp')
.controller('MainCtrl', ['$scope', '$http', 'Services', function ($scope, $http, Services) {
$scope.loadData = function () {
$scope.services = Services.query();
};
我收到这个错误: 未知提供者:ServicesProvider <- Services <- MainCtrl
您必须在您的控制器中注入服务。
var module = angular.module('frontendApp', ['ngResource']);
module.factory('MyServices', ['$resource', function($resource) {
return $resource('/services/:serviceId', {
serviceId: '@_id'
}, {});
}]);
module.controller('MainCtrl', ['$scope', '$http','MyServices', function ($scope, $http, MyServices) {
$scope.loadData = function () {
$scope.services = MyServices.query();
};
}]);