Javascript 来自html返回值的angularjs调用函数

Javascript 来自html返回值的angularjs调用函数,javascript,html,angularjs,Javascript,Html,Angularjs,我是安格拉斯的新手 我想从html调用一个函数 <td> {{getComponentSubtype(component.ID)}} </td> 这很可能并不理想,但我需要更多地了解您的代码,以获得更好的解决方案 您可以使用对象来存储模板中的类型和访问: <td>{{ componentSubtypes[component.id] }}</td> 在你的HTML中 <td>{{myvariable}}</td>

我是安格拉斯的新手

我想从html调用一个函数

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

这很可能并不理想,但我需要更多地了解您的代码,以获得更好的解决方案

您可以使用对象来存储模板中的类型和访问:

<td>{{ componentSubtypes[component.id] }}</td>
在你的HTML中

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

从HTML调用函数,收到回调后,将其值存储在JSON对象中,该对象可以用HTML打印。同时在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>
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";
    });
}

注意:我假设在HTML中,您从循环(
ng repeat
)中获得
组件

这是在
ng repeat
中调用的吗?需要更多的信息才能给您一个可靠的答案。是的,这是在ng repeat中。在这种情况下,您将需要一个类似于S4beR建议的解决方案。如果这很重要,它在ng repeat中,但根据您的建议,我不明白getComponentSubtype方法是如何被调用的相应地编辑了答案
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);

});
{{ getComponentSubtype(component.ID) }}

<td ng-if="componentsSubType[component.ID] != null">
    {{ componentsSubType[component.ID] }}
</td>
<td ng-if="componentsSubType[component.ID] == null">Loading Component ...</td>
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";
    });
}