1

我开始使用指令,现在我正在尝试使用权限级别呈现按钮。

假设我想渲染这个按钮

<a ui-sref="#" data-toggle="modal" data-target="#modalCadastro"><div id="addButton" class="btn btn-success">Add</div></a>

指示:

.directive('test', function($http){
$http.get('system/test/gettemplate')
.then(function(response){
    if(response.data.success){ // small verification to know if it worked and i got a string with the template
        $scope.template = response.data.template;
    }
})
return{
    restrict: 'AE',
    scope:{},
    template: $scope.template // which is my template that comes from back-end
}
})

我知道我错过了一些非常小的东西,但我是 Angular 的新手。

4

1 回答 1

2

您应该在通话中返回.then您的模板。$http这是一个异步调用,因此当尝试 时return { ... }$scope.template可能尚未设置。

.directive('test', function($http) {
    $http.get('system/test/gettemplate').then(function(response) {
        if(response.data.success) {
            $scope.template = response.data.template;
            return {
                restrict: 'AE',
                scope:{},
                template: $scope.template
            }
        }
    });
});
于 2017-05-17T08:51:01.720 回答