4

I'm new to AngularJS.

I want to call a function from html.

<td>
    {{getComponentSubtype(component.ID)}}
</td>

However, the function calls a webapi and waits for a callback. How do i get the data to show up in the html?

function getComponentSubtype(componentId) {
    apiService.get('/api/components/' + componentId + '/componentSubtype', config,
        getComponentSubtypeCompleted,
        getComponentSubtypeFailed);
}

function getComponentSubtypeCompleted(result) {
    $scope.ComponentSubtype = result.data;
    //////I WANT TO RETURN $scope.ComponentSubtype.Name//////
}
3
  • Is this getting called inside an ng-repeat? A little more info is required to give you a solid answer. Commented Apr 1, 2016 at 21:18
  • Yes this <td> is inside a ng-repeat Commented Apr 1, 2016 at 21:24
  • In that case you will need a solution similar to the one S4beR suggests. Commented Apr 1, 2016 at 22:09

3 Answers 3

4

call the function from HTML and once callback is received store its value in a JSON object which can be printed in HTML. Meanwhile show a loading message in UI

HTML:

{{ getComponentSubtype(component.ID) }}

<td ng-if="componentsSubType[component.ID] != null">
    {{ componentsSubType[component.ID] }}
</td>
<td ng-if="componentsSubType[component.ID] == null">Loading Component ...</td>

Controller:

function getComponentSubtype(componentId) {
    apiService.get('/api/components/' + componentId + '/componentSubtype', config,
    function(result) {
        if ($scope.componentsSubType == null) {
            $scope.componentsSubType = {};
        }

        $scope.componentsSubType[componentId] = result;
    },
    function() {
        if ($scope.componentsSubType == null) {
            $scope.componentsSubType = {};
        }

        $scope.componentsSubType[componentId] = "Load Failed";
    });
}

Note: I have assumed that in HTML you are getting component from a loop (ng-repeat)

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

Comments

1

in your HTML ...

<td>{{myvariable}}</td>

in your controller ...

angular.module('yourApp').controller('ControllerName', function($scope, apiService) {
$scope.myvariable = 'please wait';
function initComponent(id) {
    apiService.get('/api/components/' + id + '/componentSubtype').then(function(response) {
        $scope.myvariable = response;
    }).catch(function(failedResponse) {
        // handle failure here
        console.log('oops', failedResponse);
    });
}

initComponent(500);

});

Comments

0

This is quite likely not ideal, but I would need to know more about your code for a better solution.

You could use an object to store the types and access that in your template:

<td>{{ componentSubtypes[component.id] }}</td>

Call getComponentSubtype for each component id:

$scope.componentSubtypes = {};

components.forEach(function(component) {
    getComponentSubtype(component.id);
});

function getComponentSubtype(componentId) {
    apiService.get('/api/components/' + componentId + '/componentSubtype', config,
        getComponentSubtypeCompleted(componentId),
        getComponentSubtypeFailed);
}

// Using a closure here for the id
function getComponentSubtypeCompleted(id) {
    return function(result) {
       $scope.componentSubTypes[id] = result;
       // ...
   }
}

2 Comments

the <td> is within a ng-repeat if that matters, but with your suggestion, I do not understand how the method getComponentSubtype gets invoked
Edited the answer accordingly

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.