并且错误显示未知范围提供程序

并且错误显示未知范围提供程序

本文介绍了将输入字段值传递给 angularjs $resource undefined 并且错误显示未知范围提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

更新 1固定代码产生新的错误

ReferenceError: inputName 未定义

就行

 inputName:inputName,

下面是新代码

<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><脚本>var app= angular.module('myApp', ['ngRoute', 'ngResource']);app.factory('Greeter', ['$resource',function($resource){返回$资源('http://123.com/processor.php',{输入名称:输入名称,回调:'JSON_CALLBACK'},{查询:{方法:'GET',isArray:true}});}]);应用程序.controller('MyCtrl', ['$scope', 'Greeter',功能($范围,迎宾){/*警报(是");*/$scope.greet = function(){//警报(greetttt");alert("在迎宾之前"+$scope.inputName);问候语.query({inputName:$scope.inputName},功能(响应){警报(响应 [0].myCodeId);$scope.output=response[0].myCodeId;});};}]);<div ng-app="myApp"><div ng-controller="MyCtrl">你的名字:<input type="text" ng-model="inputName" name="myInput" value="World"/><button ng-click="greet()">greet</button><div>在这里测试输出{{输出}}

我不知道我哪里弄错了?

谢谢

解决方案

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

我修复了其他人在评论中指出的一些问题.

从工厂中删除 $scope.在这里你得到一个通用的 $scope 对象,但不是实际的范围.你会在控制器中得到它.当您使用 query() 调用 angular 资源时,第一个参数已经是参数.但是您可以像以前一样指定常用参数.

function($resource) {返回 $resource('mocked-resource.json', {回调:'JSON_CALLBACK'}, {询问: {方法:'获取',isArray: 真}});

希望这会有所帮助.

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

Update 1Fixed code produce new error of

ReferenceError: inputName is not defined

on the line of

 inputName:inputName,

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?

Thanks

解决方案

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.

这篇关于将输入字段值传递给 angularjs $resource undefined 并且错误显示未知范围提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:22