[英]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.