简体   繁体   English

为什么我的服务先运行?

[英]Why is my service running before anything else?

I'm new to services, factories, etc; 我是服务,工厂等的新手; so there's still a lot I don't understand. 所以还有很多我不明白的地方。

I have a ui-grid. 我有一个用户界面。 When a row is selected, I want to use that data as a param in a service to get REST data to populate another grid. 选择一行后,我想将该数据用作服务中的参数,以获取REST数据以填充另一个网格。

This is what I think it should be doing: 我认为这是应该做的:

gridOne is registered => Row Selected => Send selectedRow.id to Service => Service GETs data => data populates grid 2 gridOne已注册=>已选择行=>将selectedRow.id发送给Service =>服务GETs数据=>数据填充网格2

This is what its actually doing: 这实际上是在做什么:

Service GETs data => Error because selectedRow.id is not defined. 服务GET数据=>错误,因为未定义selectedRow.id

01| $scope.gridOne.onRegisterApi = function(gridApi){
02|     $scope.gridOneApi = gridApi
03|     $scope.gridOneApi.selection.on.rowSelectionChanged(null, function(row){
04|         $scope.gridOneSelectedRow = $scope.gridOneApi.selection.getSelectedRows()[0]
05|
06|         // v---Breakpoint on this line triggered before any grid is built---v
07|         myService.getAllObjects($scope.gridOneSelectedRow.id).then(response => {
08|             $scope.grid2.data = response
09|         }
10|     })
11| }

My service looks like this: 我的服务如下所示:

app.service('myService', function ($http) {

    return {
        get: getObjects
    }

    function getOjects(id) {
        let url = `http://${domain}/object/${id}`
        return $http.get(url).then(response => {
            return response
        }).catch(error => {
            return error
        })
    }
}

Why is the service function running before everything else? 为什么服务功能先运行?

if you are writing a service, you should not return a object/function/something, your implementation function is used as a constructor to newed up and create instance of your service. 如果您正在编写服务,则不应返回对象/函数/内容,而是将实现函数用作构造函数,以更新和创建服务实例。

so if you want to use a service, a sample for your myService will be 因此,如果您想使用服务,则myService的示例将是

app.service('myService', function ($http) {
    function getOjects(id) {
        //you should properly replace your domain, may be with the same domain by default if you start from object/...
        let url = `http://${domain}/object/+ id`
        return $http.get(url).then(response => {
            return response.data
        }).catch(error => {
            return error
        })
    }
    this.getAllObjects = getOjects;
})

in case of factory 如果是工厂

app.factory('myService', function ($http) {
    function getOjects(id) {
        //you should properly replace your domain, may be with the same domain by default if you start from object/...
        let url = `http://${domain}/object/+ id`
        return $http.get(url).then(response => {
            return response.data
        }).catch(error => {
            return error
        })
    }
    return {getAllObjects: getOjects};
})

and in the injecting end you don't need to change the code the way you are using it to load data, just wrote the code in sync of use and also, I wnder, why you are trying to select before the data is loaded, and inside the handler of row selection you want to call the data when now row is present at all, if the data is not loaded, right? 在注入端,您不需要更改使用它来加载数据的方式的代码,只需在使用同步时编写代码,而且,我想知道为什么要在加载数据之前进行选择,并且在行选择处理程序内部,当现在根本没有行时,您想调用数据,如果没有加载数据,对吗? I hope you are loading data for another grid grid2 on selection of a row of grid1 , and then want to load corresponding data related to that row to grid2 . 我希望您在选择了grid1的行时正在为另一个网格grid2加载数据,然后希望将与该行相关的相应数据加载到grid2

Feel free to comment, whatever doubt you still have. 如有任何疑问,请随时发表评论。

Good Luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM