0
 <script src="/library/angularjs/1.2.0-rc.3/angularjs.js"></script>
<script src="/library/angularjs/1.2.0-rc.3/angular-route.js"></script>
<script src="/library/angularjs/1.2.0-rc.3/angular-resource.js"></script>
<script>
var app= angular.module('myApp', ['ngRoute', 'ngResource']);
app.factory('Greeter', ['$scope' , '$resource',function($scope,$resource){
  return $resource(
    'http://adam.shopkeeper.tekserve.com/awh/ngresource/processor.php',
    {
      myvar:$scope.inputName,
      callback: 'JSON_CALLBACK'

    },
    {
      query: {method:'GET',isArray:true}
    });
}]);

app
.controller('MyCtrl', ['$scope', 'Greeter',
  function($scope,Greeter){
  /*alert("yes");*/
  $scope.greet = function(){
    //alert("greetttt");
    alert("before greeeter"+$scope.inputName);
    Greeter.query({inputName:$scope.inputName}, function(response){
      alert(response[0].myCodeId);
      $scope.output=response[0].myCodeId;
    });
  };
}]);
</script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
  Your name:
    <input type="text" ng-model="inputName" name="myInput" value="World"/>
    <button ng-click="greet()">greet</button>

  
  <div>
  Test Output Here

    {{output}}

  </div>
  </div>
</div>

The php file expect to return a single array item (i.e. item[0]), But that was not the problem. Because the $scope seems is not recognized for the inputName field, which I made it a model (though I am not sure if its the right thing to do). The error message will show below:

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- Greeter

I wonder where do I get it wrong?

7
  • Remove $scope dependency from your factory. You can't access to controller $scope from the factory (only to the $rootScope) Commented Aug 1, 2014 at 15:18
  • @Umidbek Karimov I guess the way I do it has created a seperated scope inside controller itself? Commented Aug 1, 2014 at 15:19
  • I am not sure then how could I reference the inputName model inside the factory in this case? Commented Aug 1, 2014 at 15:23
  • Controller has created own $scope, but factory doesn't need this scope, you can pass any parameter to the $provider from your controller (even $scope), but can't get access to controller variables from factory. Commented Aug 1, 2014 at 15:25
  • @Umidbek Karimov I updated the code, but new error emerged, as I expected, trouble on recognize the value of inputName. Where is the mistake? Commented Aug 1, 2014 at 15:31

1 Answer 1

1

http://plnkr.co/edit/CKgWrson3IbMugRKdX5p?p=preview

A few problems that I fixed that others pointed out in the comment.

Remove $scope from factory. Here you are getting a generic $scope object but not the actual scope. You will get that in the controller. When you call angular resource with query() the first argument is already the param. But you can specify the common params like you did before.

function($resource) {
  return $resource('mocked-resource.json', {
    callback: 'JSON_CALLBACK'
  }, {
    query: {
      method: 'GET',
      isArray: true
    }
  });

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder if I want the url dynamically respond to the $resource callback (i.e. return result into a page if I type in abc.com/log.php?var=1), should I use it to combine with ngRoute module? or just ngRoute module is enough? Do you know any similar exmaple or demostration on this? Thanks a lot!
@Chen I am not sure exactly what you are trying to accomplish but I am thinking that you want to change part of your page on that response. Changing part of the page can be done in various ways - perhaps you open a new question or go through ANgularJS docs...you can achieve that using ng-switch, ng-if, ng-view (routes) etc. Use the simplest solution but put down your uses cases on table for best decision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.