简体   繁体   English

我如何将数据从Django模板传递到Angle 4?

[英]How can i pass data from django template to angular 4?

I am having angular 4 application integrated with django. 我有与Django集成的Angular 4应用程序。 Angular 4 is just for showing some data in django template and i am having some RESTful api in django.I have used webpack to compile .ts files to js and included in django templates.I am getting the angular content in django templates and it works fine. Angular 4仅用于显示Django模板中的一些数据,并且我在Django中有一些RESTful API。我使用webpack将.ts文件编译为js并包含在django模板中。精细。

I am having angular component code 我有角度组件代码

import { Component,OnInit } from '@angular/core';
import {Http, Response} from '@angular/http';
@Component({selector: 'my-component',
            template: '<ul>
                       <li *ngFor="let res in results">
                        {{res}}
                       </li></ul>'
})
export class MyComponent {
results: {};
constructor(private http: Http) {

}
app_func(){
    this.http.get('http://someurl/id/test.json').subscribe( res => {
        this.results = res.json();
    })
}
ngOnInit(): void {
this.app_func();
}

And django template 和Django模板

{% block bundles %}
    {% render_bundle 'main' %}
{% endblock bundles %}

{% block breadcrumbs %}
{{ block.super }}
{% breadcrumb test.name 'detail' test.id %}
{% endblock breadcrumbs %}

{% block content %}
<div class="row">
   <div class="col-md-12 col-md-offset-0">
    <div class="test" id="results">
        <my-component>Loading...<my-component>
    </div>
   </div>
 </div>
{% endblock content %}

I am getting the angular templates when i am using the static url (ie exact url) in angular component. 当我在角度组件中使用静态网址(即精确网址)时,我正在获取角度模板。

But i need to pass the {{test.id}} (which is from django views) value to angular component and use this id in url to get json,because its dynamic url not static.How can i do that any ideas?? 但是我需要将{{test.id}}(来自django视图)值传递给有角度的组件,并在URL中使用此ID来获取json,因为它的动态URL不是静态的。我该怎么做?

In angular component file you can make the method as public and use it in the js file or inside script tag in html to pass the data or value.Call the function after the compiled js bundle is loaded. 在角度组件文件中,您可以将方法设为公共方法,并在js文件或html中的script标签内使用该方法以传递数据或值。在加载已编译的js捆绑包后调用函数。

Angular component code 角度组件代码

import { Component,OnInit,NgZone,OnDestroy } from '@angular/core';
import {Http, Response} from '@angular/http';
@Component({selector: 'my-component',
        template: '<ul>
                   <li *ngFor="let res in results">
                    {{res}}
                   </li></ul>'
})
export class MyComponent {
results: {};
constructor(private http: Http,private zone:NgZone) {}
app_func(test_id){
this.zone.run(() =>
    this.http.get('http://someurl/'+test_id+'/test.json').subscribe( res => 
    {
        this.results = res.json();
    })
);
}
ngOnInit(): void {
(<any>window).app_func = this.app_func.bind(this);
}
ngOnDestroy() {
(<any>window).app_func = null;
}

And django template 和Django模板

{% block bundles %}
{% render_bundle 'main' %}
{% endblock bundles %}

{% block breadcrumbs %}
{{ block.super }}
{% breadcrumb test.name 'detail' test.id %}
{% endblock breadcrumbs %}

{% block scripts %}
<script>
app_func(test_id='{{test.id}}');
</script>
{% endblock scripts %}

{% block content %}
<div class="row">
<div class="col-md-12 col-md-offset-0">
<div class="test" id="results">
    <my-component>Loading...<my-component>
</div>
</div>
</div>
{% endblock content %} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种特定情况下,如何将数据从Django中的模板传递到Javascript - How can i pass data from template in Django to Javascript in this specific case 如何在模板中将数据从 Flask 传递到 JavaScript? - How can I pass data from Flask to JavaScript in a template? 如何从列表视图模板传递数据以使用angular来编辑视图模板 - How do I pass data from the list view template to edit view template using angular 如何将 django 中的数据从 html 模板传递给 js - How to pass data from django to js from html template 我该如何将角度为2+的Map从html模板传递到自定义指令? - How can i pass a Map in angular 2+ from html template to custom directive? 如何将预先生成的数组按角度传递给模板中的组件? - How can I pass pre generated array to component in template in angular? 我如何在angular js模板中传递范围 - How can i pass scope in template in angular js 从模板传递数据以在Django中查看 - Pass data from template to view in Django 如何将Javascript变量数据传递给Template Toolkit? - How can I pass Javascript variable data to Template Toolkit? 如何将 FormControl 传递给模板中的函数? - How can I pass a FormControl to a function from the template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM