- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我尝试将 Angular 2 集成为我的 Django 应用程序的前端,但我遇到了一个问题,当我尝试创建 GET 请求时,Angular 2 向我抛出此错误(由于格式原因无法复制和粘贴) ):
这是我们用来实现此操作的三个文件:
html
{% extends 'profile/base_profile.html' %}
{% load humanize_tags %}
{% load article_tags %}
{% load staticfiles %}
{% block body %}
{% include '_includes/_article-modal-container.html' %}
<div class="container" style="border:1px solid #bfbfbf">
<!-- Load depedencies -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.1/es6-sham.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/angular2-polyfills.js"></script>
<script src="https://npmcdn.com/typescript@1.7.5/lib/typescript.js"></script>
<script src="https://npmcdn.com/systemjs@0.19.8/dist/system.src.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/Rx.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/angular2.dev.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/http.dev.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.17/router.dev.js"></script>
<!-- User defined component -->
<http-app></http-app>
<script>
System.config({
transpiler: 'typescript',
packages: {
app: {
format: 'register',
defaultExtension: 'ts'
}
}
});
System.import('{% static '_dev/a2_get_post/app.component.ts' %}')
.then(null, console.error.bind(console));
</script>
</div>
{% endblock body %}
app.component.ts
//import the Component and bootstrap from angular
import {bootstrap} from 'angular2/platform/browser';
import {Component} from 'angular2/core';
import {DataService} from './data.service.ts';
import {error} from "util";
import {HTTP_PROVIDERS} from "angular2/http";
import {Injectable} from 'angular2/core'
@Component({
selector: 'http-app',
template: `
<div>
<div class="input">
<label for="title">Title</label>
<input type="text" id="title" #title>
</div>
<div class="input">
<label for="body">Body</label>
<input type="text" id="body" #body>
</div>
<div class="input">
<label for="user-id">User ID</label>
<input type="text" id="user-id" #userId>
</div>
<button (click)="onPost(title.value, body.value, userId.value)">Post Data</button>
<button (click)="onGetPosts()">Get All Posts</button>
<p>Response: {{response}}</p>
</div>
`,
providers: [DataService]
})
@Injectable()
export class AppComponent{
response: string;
constructor ( private _dataService: DataService) {
console.log('Constructor:');
console.log(this);
console.log('');
}
onPost(title: string, body: string, userId: string) {
console.log('onPost:');
console.log(this);
console.log('');
const data = {
title: title,
body: body,
userId: userId
};
this._dataService.postData(data)
.subscribe(
data => this.response = JSON.stringify(data),
error => console.log(error)
);
}
onGetPosts() {
console.log('onGetPosts:');
console.log(this);
console.log('');
this._dataService.getData()
.subscribe(
data => {
response = JSON.stringify(data);
},
error => console.error(error)
)
}
}
bootstrap(AppComponent, [HTTP_PROVIDERS]);
data.service.ts
import {Injectable} from "angular2/core";
import {Http, Headers} from "angular2/http";
import 'rxjs/Rx';
import {Observable} from "rxjs/Observable";
@Injectable()
export class DataService {
constructor (private _http: Http) {
}
postData(data: any): Observable<any> {
const body = JSON.stringify(data);
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this._http.post('<REMOVED>', body, {headers: headers})
.map(response => response.json());
}
getData(): Observable<any> {
console.log(this);
return this._http.get('<REMOVED>')
.map(response => response.json());
}
}
Django 1.9.6、Angular 2 Beta 17、Python 3.4.3
有什么建议吗?
最佳答案
尝试使用变量 this._dataService
代替类型 DataService
this._dataService.postData(data)
.subscribe(
data => this.response = JSON.stringify(data),
error => console.log(error)
);
this._dataService.getData()
.subscribe(
data => {
response = JSON.stringify(data);
},
error => console.error(error)
)
另请参阅相应的 plnkr https://plnkr.co/edit/WemJ7qotAISH1mBvraiD?p=preview
关于javascript - Django 和 Angular 2 错误 {dataService : undefined},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37197587/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!