简体   繁体   English

angularjs http只工作一次

[英]angularjs http works only once

I saw some similiar posts at forum but didn't find solition to my problem. 我在论坛上看到了一些类似的帖子,但没有找到解决我的问题的方法。 Maybe it's because I'm new to js/angular and that stuff. 也许是因为我是js / angular之类的新手。 Here's the thing: 这是东西:

Perfect picture: I click button data (some text / numbers) from server are updated and displayed. 完美图片:我单击并更新了服务器上的按钮数据(一些文本/数字)。

My problem is - I get the data only once. 我的问题是-我仅获得一次数据。 Later some other function from jquery.js is called instead of mine. 后来,从jquery.js调用了其他函数,而不是我的。

        if ( !(eventHandle = elemData.handle) ) {
        eventHandle = elemData.handle = function( e ) {
            // Discard the second event of a jQuery.event.trigger() and
            // when an event is called after a page has unloaded
            return typeof jQuery !== core_strundefined && (!e || jQuery.event.triggered !== e.type) ?
                jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
                undefined;
        };

my code: 我的代码:

controllers.js: controllers.js:

    myApp.controller('ajaxController', function($scope, $http, $log){

        $scope.ajaxData={};
        $scope.ajaxData.doClick = function(item, event){

            $http({method: 'GET', url: 'temp/temp.json', params: { 'foobar': new Date().getTime() } }).
                success(function(data, status, headers, config) {
                    $log.debug(data);
                    $scope.ajaxData = data;
                }).
                error(function(data, status, headers, config) {
                    $log.debug("not good");
                });

index.html: index.html:

<div class="container" ng-controller="ajaxController"> (...)

<button ng-click="ajaxData.doClick(item, $event)">Send Request</button>
percentage: {{$scope.ajaxData.data.cpu}}</br>
usage: {{$scope.ajaxData.total}}</br>
idle: {{$scope.ajaxData.idle}}</br>

I can see in chrome console that first request is done and it succeed, later code mentioned above is executed instead and i get no data. 我可以在chrome控制台中看到第一个请求已完成并且成功,随后执行了上述稍后的代码,而我没有任何数据。

In your success function you're overwriting the ajaxData with your response 在您的成功函数中,您的响应将覆盖ajaxData

$scope.ajaxData = data

This means $scope.ajaxData.doClick no longer exists. 这意味着$scope.ajaxData.doClick不再存在。 Try binding doClick just to your $scope instead. 尝试将doClick仅绑定到$ scope。

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

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