简体   繁体   English

AngularJS $ http没有触发来电

[英]AngularJS $http not firing get call

I have a question about firing $http.get from multiple sources in AngularJS. 我有一个关于从AngularJS中的多个源触发$http.get的问题。 Code below is quite simple: I have $scope.test function which is click handler for one button in HTML. 下面的代码非常简单:我有$scope.test函数,它是HTML中一个按钮的单击处理程序。 This $http.get works ok. 这个$http.get工作正常。 Then I have $http.get which gets some data from server and creates basic primitives chart. 然后我有$http.get从服务器获取一些数据并创建基本原语图表。 Very simple and this works as well. 非常简单,这也很有效。 And then, I would like to append button on every chart node and on button handler I would like to execute another $http.get call. 然后,我想在每个图表节点和按钮处理程序上添加按钮我想执行另一个$http.get调用。 But this one doesn't work! 但这一个不起作用!

Here is code: 这是代码:

$scope.test = function () {
    console.log('Klic na ID 1');
    $scope.commonController.getData('orgunit/1?jsonDepth=3')
        .success(function(workpositionData,status,headers,config) {
            console.log('Klic na ID 1 OK');
            $scope.workPositions = workpositionData.workPositions;
        }).error(function(data,status,headers,config) {
            commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
        });
};


var options = new primitives.orgdiagram.Config();    
var itemB, itemC, itemD, itemE;
var rootItem = new primitives.orgdiagram.ItemConfig();

options.rootItem = rootItem;
options.cursorItem = rootItem;
options.hasSelectorCheckbox = primitives.common.Enabled.True;

var buttons = [];
buttons.push(new primitives.orgdiagram.ButtonConfig("add", "ui-icon-folder-open", "Add0"));     
options.buttons = buttons;

options.onButtonClick = function (e, data) {
    console.log('Klic na ID '+data.context.id);
    $http.get('proxy/api/orgunit/' + data.context.id + '?jsonDepth=3')
    .success(function(workpositionData,status,headers,config) {
        console.log('Klic na ID '+data.context.id + ' OK');
        $scope.workPositions = workpositionData.workPositions;
    }).error(function(data,status,headers,config) {
        commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
    });                 
};

$http.get('proxy/api/orgunit/tree?jsonDepth=2')
.success(function(orgUnitsData,status,headers,config) {
    console.log('Reading orgunit tree ok');

    rootItem.title = orgUnitsData.orgUnits[0].title;
    rootItem.description = orgUnitsData.orgUnits[0].description;        
    rootItem.id = orgUnitsData.orgUnits[0].id;
    rootItem.hasSelectorCheckbox = false;
    rootItem.image = "http://www.basicprimitives.com/demo/images/photos/a.png";

    $scope.addItems(rootItem, orgUnitsData.orgUnits[0].subordinates, 0);
    jQuery(".basicdiagram").orgDiagram(options);


}).error(function(data,status,headers,config) {
    console.log('Reading orgunit not ok');
}); 

I tried a lot of combinations of creating this chart (directive, separate template and controller, ...) but nothing works. 我尝试了很多组合创建这个图表(指令,单独的模板和控制器,......)但没有任何作用。 $http.get call from button on chart note doesn't fire (nothing in Network in Chome Developer Tools). 来自图表注释按钮的$ http.get调用不会触发(Chome Developer Tools中的网络中没有任何内容)。

But here is interesing this: if I execute test function another time (click on html button), I get response from test function AND from $http.get from chart button. 但是这里是这样的:如果我再次执行测试功能(点击html按钮),我会从测试功能来自图表按钮的$ http.get获得响应。 It looks like $http.get call from chart button is waiting for something and when this something appers, it fires. 它看起来像是来自图表按钮的$ http.get调用正在等待什么东西,当这个东西出现时,它会触发。

Does anyone have any idea what would be solution to this problem? 有谁知道这个问题的解决方案是什么? Output in console for scenario execute test, execute chart button function, execute test is like this ( bolded are console entries from chart button function , nonbolded from test function: 在控制台中输出用于场景执行测试,执行图表按钮功能,执行测试是这样的( 粗体是来自图表按钮功能的控制台条目 ,来自测试功能的非粗体

Klic na ID 1 Klic na ID 1 OK Klic na ID 4 Klic na ID 1 Klic na ID 1 OK Klic na ID 4 OK Klic na ID 1 Klic na ID 1 OK Klic na ID 4 Klic na ID 1 Klic na ID 1 OK Klic na ID 4 OK

If you have any idea about this please let me know, this thing is driving me crazy a few last hours. 如果您对此有任何疑问请告诉我,这件事让我疯狂了几个小时。 UPDATE UPDATE

I solved it with solution, found here https://github.com/angular/angular.js/issues/2794#issuecomment-18807158 , so I wraped my call function with $scope.$apply. 我用解决方案解决了它,在这里找到https://github.com/angular/angular.js/issues/2794#issuecomment-18807158 ,所以我用$ scope包装了我的调用函数。$ apply。

$scope.$apply(function() {
    console.log('Klic na ID ' + data.context.id);
    $scope.commonController.getData('orgunit/' + data.context.id + '?jsonDepth=3')
    .success(function(workpositionData,status,headers,config) {
        console.log('Klic na ID ' + data.context.id + ' OK');
        $scope.workPositions = workpositionData.workPositions;
    }).error(function(data,status,headers,config) {
        commonController.error('Pri branju delovnih mest je prišlo do napake: '+data.description);
    });
});

Best Regards 最好的祝福

Starting from angular 1.1.4 you need to call $http in the context of an angular digest loop. 从角度1.1.4开始,您需要在角度摘要循环的上下文中调用$ http。 If you do not do so then you can manually call $scope.$apply(); 如果你不这样做,那么你可以手动调用$scope.$apply(); after the http call. http调用之后。 See https://github.com/angular/angular.js/issues/2431#issuecomment-18566595 请参阅https://github.com/angular/angular.js/issues/2431#issuecomment-18566595

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

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