传递一个输入字段值到angularjs $资源不确定和错误显示未知范围提供商 [英] Pass a input field value into angularjs $resource undefined and error shows unknown scope provider

查看:335
本文介绍了传递一个输入字段值到angularjs $资源不确定和错误显示未知范围提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

=============================================== ============================

===========================================================================

更新1
固定code产生的新的错误

Update 1 Fixed code produce new error of

ReferenceError: inputName is not defined

这行

 inputName:inputName,

下面是新的code

Below is the new code

<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', ['$resource',function($resource){
  return $resource(
    'http://123.com/processor.php',
    {
      inputName: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>

我不知道我在哪里弄错了吗?

I wonder where do I get it wrong?

感谢

推荐答案

的http:/ /plnkr.co/edit/CKgWrson3IbMugRKdX5p?p=$p$pview

这是我固定的人在评论中指出的几个问题。

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

从工厂删除$范围。在这里,你得到一个通用$范围对象,但不是实际的范围。你会得到在控制器中。
当你调用角资源与查询()第一个参数是已经帕拉姆。但是像你这样才可以指定一个普通PARAMS。

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
    }
  });

希望这有助于。

这篇关于传递一个输入字段值到angularjs $资源不确定和错误显示未知范围提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆