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

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

问题描述

非 GET 实例操作 $save 在我的示例中不起作用.我总是得到错误,即 $save 不是一个函数.问题是,我不知道我必须在哪里定义 $scope.example = new Resource();,因为在我的示例中我使用了 2 个控制器.一个用于带有对象的表列表,另一个用于我的模态窗口,您可以在其中进行 CRUD 操作.CRUD 操作在 angular 服务中定义.

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.

代码结构如下:

资源服务:

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

CRUD 服务:

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

模态窗口的Ctrl:

$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.'
            },
...];

带有表单的视图:

<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>

我收到一个新错误.控制台告诉我必须使用 track by 表达式.但是我试图在不生成的情况下使用表单视图然后工作.但我需要生成的表单视图(上面的示例视图).

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).

错误信息:

错误:ngRepeat:dupes中继器中的重复键

Error: ngRepeat:dupes Duplicate Key in Repeater

不允许在中继器中重复.使用track by"表达式指定唯一键.

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;

});

在工厂返回的所有东西都是可用的.
该服务在调用它时会自动创建一个新对象,从而使该对象(t​​his")可用

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

编辑

另请注意,您可以决定您的承诺是否会进入下一个错误或成功调用.

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

举个例子:

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

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

成功和错误都是回调函数.通过使用 $q 您可以转到下一个成功或错误,无论回调如何.

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

问题代码

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

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

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