0

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. 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.

        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:

    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:

<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.

1 Answer 1

3

In your success function you're overwriting the ajaxData with your response

$scope.ajaxData = data

This means $scope.ajaxData.doClick no longer exists. Try binding doClick just to your $scope instead.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right! I changed it in different way - instead $scope.ajaxData = data; --> $scope.ajaxData.someData = data.someData; and it works. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.