AngularJS工厂的休息呼叫错误



我是AngularJS的新手

这是我的代码

工厂

'use strict';
angular.module('restPracticeApp')
  .factory('myFactory',['$resource', function () {

    console.log('hi from factory')
  function myFactory($resource) {
    var myUrl = 'api/theUrl';
  return $resource(myUrl,{} , {
      'query': { method: 'GET', isArray: true},
      'get': { method: 'GET'}
        });  
  }
  }]);

控制器:

'use strict';

angular.module('restPracticeApp')
  .controller('AboutCtrl',['$scope','$resource','$http','myFactory' ,
    function ($scope,$resource,$http,myFactory) {
        $scope.theLink = theLink;
        function theLink(theName) {
            $http({
                    method: 'GET',
                    url: "http://localhost:9000/api/theUrl",
                    data: $scope.theName
            })
            .then(
                function successCallback(response) {
                    console.log(response);
                    $scope.result=response;
                },
                function errorCallback(response) {
                    console.log(response);
                    $scope.result=response;
              });
        }
  }]);

和html

<input type="text" ng-model="theName">
<button class="btn btn-block" ng-click="theLink(theName)">yo</button>

我正在尝试在工厂返回,但仍然说" 提供者'myFactory'必须从$ Get Factory方法返回值。"

请帮助我。我将非常感谢

尝试一下:

factory("myFactory",function($resource){
    var factory = {};
    factory.getResources = $resource('api/theUrl', {}, {
        'query': {method: 'GET', isArray: true}
    });
    return factory;
})

控制器有关如何接收工厂数据的示例:

.controller("AboutCtrl",function($scope,$resource,$http,myFactory){
    $scope.factorydata = myFactory.getResources.query();
    $scope.factorydata.$promise.then(function(data) {
        //something you want to do
    })
}) 

最新更新