Angularjs Restful using ngResource 不起作用

Angularjs Restful using ngResource not working

本文关键字:不起作用 ngResource using Restful Angularjs      更新时间:2023-09-26

我正在尝试使用 angularjs ngResource 作为我的后端公开 restful Web 服务 一切似乎都是正确的,但不知道我的代码出了什么问题浏览器中不显示任何内容,可以帮助我解决这个问题

service.js
 'use strict';
 var employeeServices = angular.module('myApp.services',['ngResource']);
 employeeServices.factory('Employees',['$resource',function ($resource){
    return $resource('my service link', {},{
        query:{method:'GET',isArray:true}
    });
 }]);
 Controller.js
'use strict';
 angular.module('myApp.controllers',[]).
 controller('Myctrl1',['$scope','Employees',function ($scope ,Employees){
    $scope.allemployees =  Employees.query();
}]).
    controller('Myctrl2',[function (){
    }]);  
app.js
'use strict';
 angular.module("myApp", [
  'ngRoute',
  'myApp.controllers',
  'myApp.services'
 ]).
 config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1',{templateUrl:'partials/partials.html',controller:'Myctrl1'});
  $routeProvider.when('/view2',{templateUrl:'partials/partials1.html',controller:'Myctrl2'});
  $routeProvider.otherwise({redirectTo: '/view1'});
 }]);
如果你想在

另一个模块中使用一个模块的服务,你必须注入它。

service.js
'use strict';
var employeeServices = angular.module('myApp.services', ['ngResource']);
employeeServices.factory('Employees', ['$resource', function ($resource) {
    return $resource('my service link', {}, {
        query: { method: 'GET', isArray: true }
    });
}]);
Controller.js
'use strict';
angular.module('myApp.controllers', ['myApp.services']).
controller('Myctrl1', ['$scope', 'Employees', function ($scope, Employees) {
    $scope.allemployees = Employees.query();
}]).
   controller('Myctrl2', [function () {
   }]);
app.js
'use strict';`enter code here`
angular.module("myApp", [
 'ngRoute',
 'myApp.controllers',
 'myApp.services'
]).
config(['$routeProvider', function ($routeProvider) {
    $routeProvider.when('/view1', { templateUrl: 'partials/partials.html', controller: 'Myctrl1' });
    $routeProvider.when('/view2', { templateUrl: 'partials/partials1.html', controller: 'Myctrl2' });
    $routeProvider.otherwise({ redirectTo: '/view1' });
}]);

试试这个