错误:$保存不是一个函数(AngularJS) [英] Error: .$save is not a function (AngularJS)

查看:147
本文介绍了错误:$保存不是一个函数(AngularJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非获得实例行动 $保存并不在我的示例工作。我总是得到错误, $保存不是一个函数。问题是,我不知道我在哪里有定义 $ scope.example =新的资源(); ,因为在我的例子中,我使用2个控制器。一个与对象表列表,另一个为我的模式窗口,在这里你可以采取CRUD操作。 CRUD操作在一个角服务定义

在code的结构如下:

资源役:

  ...
返回{
  名称:$资源(的baseUrl +'/ API /名/:身份证',{
     ID:@Id
  },{
     更新:{
        方法:把'
     }
  }),
...

CRUD的服务:

  ...
返回{
   创建:功能(了newName){
      返回了newName $保存()。
   },
...

模式窗口的Ctrl键:

  $ scope.selected =新resService.name();
$ scope.createItem =功能(了newName){
   CrudService.create(了newName)。然后(
        功能(){
           $ scope.dataSuccess ='​​人创建。';
           $ scope.newName = NULL;
        },
        功能(错误){
           $ scope.dataError = err.data.ModelState;
        });
   }
}$ scope.form = [{
                标签:'名字',
                字段类型:文本,
                名称:'FNAME',
                ID:'FNAME-ID',
                参数propertyName:FNAME',
                禁用:假的,
                模式:/ ^ [A-ZA-Z] {4} [A-ZA-Z] * /
                要求:真实,
                errRequired:姓是必需的。,
                errPattern:'姓至少有4个字母
            },
...];

与形式的观点:

 <窗​​体类=形横NAME =editFormNOVALIDATE>
  < D​​IV CLASS =表单组SM有反馈NG重复=ELEM形式NG-CLASS ={'有错误:hasError(editForm,elem.name),'有-成功 :hasSuccess(editForm,elem.name)}>
     <标签类=控制标签为={{elem.id}}> {{elem.label}}< /标签>
     <输入类型={{elem.fieldType}}
            类=表格控
            占位={{elem.label}}
            NAME ={{elem.name}}
            编号={{elem.id}}
            NG-模式=选择了[elem.propertyName]
            NG-禁用={{elem.disabled}}
            NG-模式=elem.pattern
            NG-所需={{elem.required}}
            />< p =班帮助块NG-IF =elem.errRequiredNG-秀=editForm [elem.name] $ error.required&放大器;&安培; editForm [elem.name] $感动&GT ; {{elem.errRequired}}&下; / p>
< p =班帮助块NG-IF =elem.errPatternNG-秀=editForm [elem.name] $ error.pattern。> {{elem.errPattern}}< / P>

编辑:
我得到一个新的错误。控制台告诉,我必须使用轨迹EX pression。但我试图用不生成表单视图,然后工作。但我需要生成的表单视图(上面的例子视图)。

错误消息:


  

错误:ngRepeat:易受骗的人
  在中继器重复键


  
  

在一个中继器不允许重复。使用轨道由前pression指定的唯一密钥。



解决方案

如果您wan't创建一个新的对象,你需要选择服务的选择(工厂,服务提供商)之间的服务。

工厂和服务之间的差别,是关于语法。只是语法。

  .factory(函数(){   //私有变量和函数
   VAR X =EZ;
   起作用的getX(){
        返回X;
   }   //公共函数(或变量)
   返回{
     一个测试,
     木屐:功能(){
        返回;
     }
   }})
//服务示例。服务(函数(){   //处理的由角:
   //新()被用于创建新的对象  //私有函数和变量
  VAR X =测试;
  起作用的getX(){
    返回X;
  }  //公共funcitons(和变量)
  this.a =功能(){
     测试;
  };  this.getA =功能(){
    返回;
  };  //通过AngularJS Handeled
  //返回这一点;});

这是在工厂返回的一切都可用。结果
服务automaticaly创建调用时,一个新的对象,这使得可用的对象(本)

调用一个服务或工厂仍然是相同的:

  VAR一个= service.getA();
变种一个= factory.getA();

修改

还请注意,如果你的承诺是要下一个错误或成功的呼叫,你可以决定。

,正如〔实施例:

XHR()结果
。然后(success1,ERROR1)结果
。然后(success2,误差2)结果
。然后(success3,误差3)结果
...

成功和错误都是回调函数。
通过使用 $ Q 你可以去下一个成功或错误,wathever回调。

问code

 。工厂('YourFacotry',['$资源',
       功能($资源){
          返回$资源('/ API / NOTE /:身份证',{ID:'@id'},
             {
               标为已完成 :
                 {
                   网址:'/ API / NOTE /:ID /完成',
                   方法:POST,
                   IsArray的:真
                  }
             });
        }]);
模态窗口Ctrl键:$ scope.createItem =功能(){//忘记$范围在这里!
   CrudService.query()。然后(
        功能(){
           $ scope.dataSuccess ='​​创建人';
           $ scope.newName = NULL;
        },
        功能(错误){
           $ scope.dataError = err.data.ModelState;
        });
   }
}

The non-GET instance action $save doesn't work in my example. I always get the Error, that $save is not a function. The problem is, I don't know where I have to define the $scope.example = new Resource();, because in my example I'm using 2 Controllers. One for the table list with objects and the other one for my modal window, where you can take CRUD operations. The CRUD operations are defined in an angular service.

The code is structured as follows:

Servie of Resource:

...
return {
  name: $resource(baseUrl + '/api/name/:Id', {
     Id: '@Id'
  }, {
     'update': {
        method: 'PUT'
     }
  }),
...

Service of CRUD:

...
return {
   create: function (newName) {
      return newName.$save();
   },
...

Ctrl of modal window:

$scope.selected = new resService.name();
$scope.createItem = function (newName) {
   CrudService.create(newName).then(
        function () {
           $scope.dataSuccess = 'Person created.';
           $scope.newName = null;
        },
        function (err) {
           $scope.dataError = err.data.ModelState;
        });
   }
}

$scope.form = [{
                label: 'Firstname',
                fieldType: 'text',
                name: 'Fname',
                id: 'fname-id',
                propertyName: 'fname',
                disabled: false,
                pattern: /^[a-zA-Z]{4}[a-zA-Z]*/,
                required: true,
                errRequired: 'Firstname is required.',
                errPattern: 'Firstname has at least 4 letters.'
            },
...];

The view with form:

<form class="form-horizontal" name="editForm" novalidate>
  <div class="form-group-sm has-feedback" ng-repeat="elem in form" ng-class="{ 'has-error' : hasError(editForm, elem.name), 'has-success' : hasSuccess(editForm, elem.name) }">
     <label class="control-label" for="{{elem.id}}">{{elem.label}}</label>
     <input type="{{elem.fieldType}}"
            class="form-control" 
            placeholder="{{elem.label}}"  
            name="{{elem.name}}" 
            id="{{elem.id}}"
            ng-model="selected[elem.propertyName]" 
            ng-disabled="{{elem.disabled}}"
            ng-pattern="elem.pattern" 
            ng-required="{{elem.required}}"
            />

<p class="help-block" ng-if="elem.errRequired" ng-show="editForm[elem.name].$error.required && editForm[elem.name].$touched">{{elem.errRequired}}</p>
<p class="help-block" ng-if="elem.errPattern" ng-show="editForm[elem.name].$error.pattern">{{elem.errPattern}}</p>

EDIT: I'm getting a new Error. The console tells, that I have to use track by expression. But I was trying to use the form view without generating and then works. But I need the generated form view (the example view above).

Error Message:

Error: ngRepeat:dupes Duplicate Key in Repeater

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.

解决方案

If you wan't to create a new object you need the choose the service between the Services choice (factory, service, providers).

The difference between a factory and a service, is about syntax. Just syntax.

.factory(function(){

   //Private variables and functions
   var x = "ez";
   function getX(){
        return x;
   }

   //Public functions (or variables)
   return {
     a : "test",
     getA : function(){
        return a;    
     }
   }

})


//Service example

.service(function(){

   //Handled by Angular: 
   //new() is used to create a new object

  //Private functions and variables
  var x = "test";
  function getX(){
    return x;
  }

  //Public funcitons (and variables)
  this.a = function(){
     "test";
  };

  this.getA = function(){
    return a;
  };

  //Handeled by AngularJS 
  //return this;

});

Everything that is returned in the factory is available.
The service automaticaly creates a new object when calling it, which makes available the object ("this")

Calling a service or a factory remains the same:

var a = service.getA();
var a = factory.getA();

EDIT

Notice also that you can decide if your promise is going to the next error or success call.

Just as an exmaple:

xhr()
.then(success1, error1)
.then(success2, error2)
.then(success3, error3)
...

success and error are all callback functions. By using $q you can go to the next success or error, wathever the callback.

QUESTION CODE

 . factory ( 'YourFacotry' , [ '$resource' , 
       function ( $resource ) {    
          return $resource ( '/api/note/:id' , { id : '@id' }, 
             { 
               markAsDone : 
                 { 
                   url : '/api/note/:id/done' , 
                   method : 'POST' , 
                   isArray : true 
                  } 
             }); 
        }]);


Ctrl of modal window:

$scope.createItem = function () { //Forgot $scope here!
   CrudService.query().then(
        function () {
           $scope.dataSuccess = 'Person created';
           $scope.newName = null;
        },
        function (err) {
           $scope.dataError = err.data.ModelState;
        });
   }
}

这篇关于错误:$保存不是一个函数(AngularJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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