标签: angular-resource

如何从调用函数控制isArray值?(修改$ resource params)

我想知道如何设置isArray参数resource,因为有时候我希望它true有时候false

这是我的资源工厂代码

dummyServices.factory('Dummy', ['$resource',
    function ($resource) {
        return $resource('http://dummyServer/dummyApp/dummyREST/:methodName', {}, {
            query: {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                isArray: false // <-- isArray: someParameter ???
            }
        });
    }]);
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

$scope.myDummyObj = Dummy.query(
        {
            methodName: 'dummyMethod'
        },
        myDummyArgument
    );
Run Code Online (Sandbox Code Playgroud)

如何设置isArray从我的Dummy.query电话?

angularjs angular-resource

2
推荐指数
1
解决办法
1172
查看次数

使用ngResource和webpack

大家,早安,

我最近开始在一个新的角度项目中使用webpack.我非常喜欢它,非常容易使用.我现在只有一个问题.我正在尝试导入ngResource以在我的一个模块上使用,但不是我尝试工作的方式.我正在使用带有babel装载机的webpack(es6)

我尝试了以下各种方法:

//1
import ngResource from 'ng-resource'
angular.module('app', [ngResource])

//2
import 'ng-resource'
angular.module('app', ['ngResource'])


//3
import ngResource from 'ng-resource'
angular.module('app', [ngResource.name])
Run Code Online (Sandbox Code Playgroud)

我甚至尝试添加以下加载器:

{
     test: /[\/]angular-resource\.js$/,
     loader: 'exports?angular.module(\'ngResource\')'
}
Run Code Online (Sandbox Code Playgroud)

然后在我的模块上:

angular.module('app', ['ngResource'])
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我想不出来:(

先谢谢,Chaim

angularjs ecmascript-6 angular-resource webpack

2
推荐指数
1
解决办法
1806
查看次数

$ resource service .success不是一个函数

我想用AngularJS,Node和MongoDB实现登录方法.我已经在我发送请求时构建了一个Restful API.

当我尝试执行GET请求时,控制台上会出现此错误 TypeError: UserService.logIn(...).success is not a function

成功不存在的$http方式?

我也找到了这个,但我无法理解如何调整它以适应我的代码.

  • HTTP GET"类"操作:Resource.action([parameters],[success],[error])

  • 非GET"类"操作:Resource.action([parameters],postData,[success],[error])

  • 非GET实例操作:实例.$ action([参数],[成功],[错误])

Service.js

var appServices = angular.module('starter.services', ['ngResource']);

appServices.factory('UserService', function ($resource) {
    return {
        logIn: function (email, password) {
            return $resource("http://localhost:3000/users/login", {
                email: email,
                password: password
            });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

Controller.js

    var apps = angular.module('starter.controller', []);
apps.controller('loginCtrl', function ($scope, $ionicPopup, $state, UserService) {

    $scope.doLogin = function doLogin(email, password) {


        if (email != null && password != null) {

            UserService.logIn(email, password).success(function (data) {
                $state.go('tabs.home');

            }).error(function …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb angularjs angular-resource

2
推荐指数
1
解决办法
1339
查看次数

在Laravel中验证Input :: json()

验证不适用于Input :: json.我尝试过使用json_decode /使用数组的不同方法,但仍然没有运气.这是我的代码:

//routes.php
Route::get('create', function() {

    $rules = array(
        'username' => 'required',
        'password' => 'required',
    );

    $input = Input::json();
    //var_dump($input); //outputs std obj with correct inputs
    $validation = Validator::make($input, $rules);
    if ($validation->fails()) { //throws exeption "Call to a member function to_array() on a non-object"
        return Response::json($validation->errors->all());
    }

}
Run Code Online (Sandbox Code Playgroud)

我正在使用Angular Resource发布数据...但它总是抛出错误"在非对象上调用成员函数to_array()"...我无法粘贴我的角度代码,因为我无法正确格式化和stackoverflow不允许我这样做.

laravel angularjs angular-resource

1
推荐指数
1
解决办法
4685
查看次数

调用AngularJS $ save后没有得到更新的值

伙计们,我使用AngularJS和angular-resource来执行一个非常简单的REST API调用.这是我列出(并创建)消息的页面的JavaScript代码:

(function() {
'use strict';
var messagesApp = angular.module('messagesApp', ['ngResource']);

messagesApp.factory('Message', function($resource) {
    return $resource('/api/messages/:id', {id: '@id'}, {
        query: {method:'GET', isArray:true} });
});

messagesApp.controller('MessagesCtrl',
  function($scope, Message) {
    $scope.messages = Message.query();

    $scope.addMessage = function() {
        $.Dialog({
            'title': 'Add new Message',
            'content': '<p>Enter the <strong>unique</strong> name of the new message.</p><input type="text" id="newMessageName"/><p>Enter the <strong>unique</strong> ID for this message.</p><input type="text" id="newID"/>',
            'draggable': false,
            'overlay': true,
            'closeButton': true,
            'buttonsAlign': 'right',
            'buttons': {
                'save': {
                    'action' : function() {
                        var newMessage = new Message(); …
Run Code Online (Sandbox Code Playgroud)

javascript rest angularjs angular-resource

1
推荐指数
1
解决办法
3528
查看次数

新的Angular.js 1.1.5资源返回promise then()函数错误后跟另一个请求

我正在使用最新的Angular.js,1.1.5,它通过资源调用返回promise.当您有多个请求后,另一个请求依赖于这些请求时,什么是正确的实现?

$scope.save = function() {
    var newids = [];
    angular.forEach ($scope.template.children, function (value){
        //saves the children as a new template and returns the ID
        Template.$addNew(value).then(
            function( value ){newids.push(value);},
            function ( error ) {console.log ('error')}
        )
    });

    //updates the root template
    $scope.template.childrenIDs = newids;
    Template.$update($scope.template).then(
            function( value ){ console.log('successfully saved')},
            function ( error ) {console.log ('error')}
        )
}
Run Code Online (Sandbox Code Playgroud)

为此,我收到一个错误:

TypeError:对象#没有方法'然后'

模板是以下工厂返回资源:

mongoAPI.
factory('Template', ['$resource', '$http', 'CONSTANTS', 'security', function($resource, $http, CONSTANTS, security) {
    var actions = {                        
        'addNew': {method:'POST'},   
    }
    var res …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-resource q

1
推荐指数
1
解决办法
5984
查看次数

角度等待资源删除响应更新列表?

我正在使用$resource添加/删除列表.问题是删除后我仍然看到有时删除元素.

我怎么解决它?

这是代码:

services.js

var services = angular.module('cliConsApp.services', ['ngResource']);

services.factory('IndustrialistsFactory', function ($resource) {
    return $resource(
        '/app_dev.php/api/v1/constructionprivateinformations/:id/industrialists',
        {id: '@id'},
        {
            query: { method: 'GET', isArray: true },
            create: { method: 'POST'}
        }
    )
});

services.factory('IndustrialistFactory', function ($resource) {
    return $resource(
        '/app_dev.php/api/v1/constructionprivateinformations/:id/industrialists/:industrialistId',
        {id: '@id', industrialistId:'@industrialistId'},
        {
        delete: { method: 'DELETE', params: {id: '@id'} }
        }
    )
});
Run Code Online (Sandbox Code Playgroud)

controllers.js

app.controller('industrialistCtrl', ['$scope', 'IndustrialistsFactory', 'IndustrialistFactory',

function ($scope, IndustrialistsFactory, IndustrialistFactory) {

        $scope.createNewUser = function (id) {
            IndustrialistsFactory.create({id: id},$scope.industrialist);

            $scope.industrialists= IndustrialistsFactory.query({},{id: id });

        }

        $scope.deleteUser …
Run Code Online (Sandbox Code Playgroud)

data-binding angularjs angular-resource angularjs-scope

1
推荐指数
1
解决办法
3975
查看次数

如何将params传递给AngularJS工厂

我对Angular很新,但是我试图将REST $从$ http抽象到工厂/资源,但我似乎无法将任何参数传递给它.虽然我读过但是找不到这个例子.

我的工厂代码(services.js):

myApp.factory("PropertyDb", function($resource, $log) {
return {
    getProperties: function(onSuccess) {
        var properties = $resource("http://myurl.com/get_properties.php?", {
            callback: 'JSON_CALLBACK',
            postcode: 'AA11AA',
            minimum_beds: '3',
            minimum_price: '97500'
        }, 

        {
            fetch : {method:'JSONP'},
            params: {postcode: 'BB11BB'} // This doesn't override the above or work without the above postcode

        });

        properties.fetch(
            function success(response) {
                console.log(response);
                onSuccess(response.listing);
            }, 
            function error(response) {
                console.log(response);
                console.log("error");
            }
        );
    },
Run Code Online (Sandbox Code Playgroud)

我的控制器代码:

myControllers.controller('PropertyListCtrl', ['$scope', 'PropertyDb',
function($scope, PropertyDb) {

    $scope.properties = {};

    // Adding the postcode below doesnt work...
    PropertyDb.getProperties({postcode …
Run Code Online (Sandbox Code Playgroud)

angularjs angular-resource

1
推荐指数
1
解决办法
1万
查看次数

使用$ resource发布表单数据

以下代码将表单数据发布为json而不是键值对.

服务

            $resource(apiPath, {
                Id: '@Id',
                apt_id: user_info.apt_id,
                mauth_token: user_info.mauth_token,
                mauth_account_id: user_info.mauth_acnt_id,
                rest: 1,
            }, {
                save:{
                    method:'POST',
                    headers : {'Content-Type': 'application/x-www-form-urlencoded'}
                }
            });
Run Code Online (Sandbox Code Playgroud)

调节器

.controller('BroadcastSmsSendCtrl', function($scope, $ionicLoading, $ionicActionSheet, $state, $timeout, $location, BroadcastSmsSvcs, GetUserCountHttpSvcs) {
            $scope.entry = new BroadcastSmsSvcs();

            $scope.doSubmit = function() {
                BroadcastSmsSvcs.save({}, $scope.entry);
            };
        });
Run Code Online (Sandbox Code Playgroud)

表格数据

javascript angularjs angular-resource

1
推荐指数
1
解决办法
5315
查看次数

使用 $resource 来 POST 正文

如何使用POSTAngular 对有效负载主体执行法线操作$resource。现在,当我 时POST,它会发布到/api/example?name=JoeSmith&is_whatever=false,而不是通过正文发布。

假设我有以下内容:

ENDPOINT: `/api/example`
BODY: {
   "name": "Joe Smith",
   "is_whatever": false
}
Run Code Online (Sandbox Code Playgroud)

接口服务

angular.module('example')
   .factory('APIService', ['$resource',

        function($resource) {

           return $resource('/api/example', {}, {
              create: {
                 method: 'POST',
              }
           });          

        }]);
Run Code Online (Sandbox Code Playgroud)

用法示例

    // body i need to POST
    var payload = {
       name: 'Joe Smith',
       is_whatever: false        
    };

    APIService.create(payload).$promise.then(function(res){
        // doesnt work
    });
Run Code Online (Sandbox Code Playgroud)

angularjs angular-resource

1
推荐指数
1
解决办法
4864
查看次数