gpt4 book ai didi

javascript - 指令在给变量赋值之前渲染 HTML

转载 作者:行者123 更新时间:2023-11-28 05:45:22 28 4
gpt4 key购买 nike

我正在运行 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 Controller 有一个 promise ,即从 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'
}
});

某些模板内部有变量,这些变量依赖于正在解析的 Controller 的 promise 。但是,当我通过测试它时。

>>> python manage.py runserver

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

我正在做的事情可能吗?如果是这样,有解决办法吗?谢谢。

编辑:固定语法。

最佳答案

看起来您需要为该指令注册 Controller 。尝试添加:

Controller :“helloWorld Controller”到指令的返回对象。

关于javascript - 指令在给变量赋值之前渲染 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38572724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com