如何在angularjs中为dreamfactory存储过程创建一个动态api url

How to make a dynamic api url in angularjs for a dreamfactory stored procedure

本文关键字:一个 动态 url api 创建 angularjs 中为 存储过程 dreamfactory      更新时间:2023-09-26

如果我在dreamfactory中使用工厂/服务调用我的api。

FoundationApp.factory('testAPI', function($resource, ChildID) {
    return $resource('http://Domain.com/rest/RemoteDB/_proc/TimeLog_Checkin(:ChildID)/?app_name=App&fields=*', {ChildID: @ChildID}, {
        update: {
            method: 'PUT'
        }
    });
})

使用带有get函数的控件来调用用户输入的具有特定ChildID的api,我该如何更改ChildID?

.controller('testCtrl', ['$scope','$resource','$routeParams','testAPI', function($scope,$resource,$routeParams,testAPI) {
    console.log("Welcome to Test");
    $scope.TestFunction = function() {
        test.get($scope.ChildID);
    };
}])

子id由html文档获取。这是它的代码。

<div class="well">
    <button class="btn btn-default" ng-click="TestFunction()">Get all Clients</button>
    <div>
        <input type="text" class="form-control" ng-model="ChildID">
    </div>
</div>

我自己也做了一些测试,我通过将url中的ChildID替换为ChildID来寻找

http://Domain.com/rest/RemoteDB/_proc/TimeLog_Checkin(1)/?app_name=App&fields=*

它确实有效,但我需要一个动态url来处理用户输入。

我试着想办法实现这个想法,但每一个现有的例子都不适合我的情况,不足以解决我的问题。

请提供任何帮助都将是惊人的。我花了好几个小时在这上面。

$resource是一个强大的工具,但它可能太多,也可能不够灵活,无法满足您的需求。但在这种情况下,其实很简单。你只需要做

testApi.update({childId:"childId", ....})

var api = new testApi({childId:"id"}); 
api.otherProp="value"; 
api.$update();

只需将ChildId与类似的url连接起来

FoundationApp.factory('testAPI', function($resource, ChildID) {
     return $resource('http://Domain.com/rest/RemoteDB/_proc/TimeLog_Checkin(' + ChildID + ')/?app_name=App&fields=*', {ChildID: ChildID}, {
         update: {
             method: 'PUT'
         }
     });
})

我不知道@sign是什么意思,所以我删除了

哦,我错过了你没有使用任何函数来获取参数,这样做吧。你会称之为data = testAPI.getData($scope.ChildID)

FoundationApp.factory('testAPI', function($resource) {
 return { 
          getData: function(ChildID) {
                    return $resource('http://Domain.com/rest/RemoteDB/_proc/TimeLog_Checkin(' + ChildID + ')/?app_name=App&fields=*', {ChildID: ChildID}, {
                     update: {
                               method: 'PUT'
                             }
                     });
          }
       }
})