指令在给变量赋值之前呈现HTML

时间:2016-07-25 16:13:08

标签: javascript angularjs django

我正在运行Django 1.9和AngularJS 1.5。

我的Django View看起来像这样:

#hello_world_view.py
def hello_world(request):
    t = get_template('hello-world.html')
    return HttpResponse(t.render())

HTML文件如下所示:

<-- loading JavaScript files above -->
<helloWorld><HelloWorld>

AngularJS控制器具有从Django REST API检索数据的承诺。它看起来像这样

helloWorld.controller('helloWorldController', 
    function($scope, $helloWorldService1, $helloWorldService2) {
        Promise.all([$hWService1, $hWService2]).then(function(response) {
             $scope.service1 = response[0];
             $scope.service2 = response[1];
        });
    });

指令如下所示:

// helloWorld is already defined as an app.
helloWorld.directive('helloWorldDirective', function() {
    return {
        restrict:'E',
        link: function($scope, $elements, $attributes) {
            $scope.$watch('someVariable', function(data) {
                if (data) {
                    $elements.text(data);
                }
            }, true);
        },
        templateUrl: '/static/partials/some-template.html'
    }
});

某些模板中包含的变量取决于要解析的控制器的承诺。但是,当我通过测试时。

>>> python manage.py runserver

不呈现变量。我已经为angularJS将标签从{{}}更改为[[]],所以我知道Django不会意外地尝试渲染这些变量。

我正在做什么甚至可能?如果是这样,有没有修复?感谢。

编辑:修正语法。

1 个答案:

答案 0 :(得分:0)

看起来您需要为指令注册控制器。尝试添加:

controller:'helloWorldController'到指令的返回对象。