小编RVP*_*RVP的帖子

node_modules的Angular2和TypeScript导入

我有一个非常简单的'你好世界'Angular2应用程序.我做了一个明显不合理的决定,在我的开发项目和我的spring后端的最终部署文件夹之间使用不同的目录结构.

由于存在这种差异,我在使用TypeScript导入时遇到了问题,当我尝试在浏览器中打开实际应用程序时,此行最终产生404错误(无法找到/ angular2 /核心库):

import {Component, View} from 'angular2/core';
Run Code Online (Sandbox Code Playgroud)

长话短说,我最后添加了/ app文件夹以使一切正常,但我最终修改了我的import语句,如下所示:

import {Component, View} from '../node_modules/angular2/core';
Run Code Online (Sandbox Code Playgroud)

然而,这导致了一些奇怪的行为.出于某种原因../node_modules,在库路径中指定导致JS使用ajax调用从头开始实际加载所有Angular2文件,以从npm_modules/angular2/文件夹中检索每个单独的文件,即使这是我的HTML标头的一部分:

<script src="/node_modules/angular2/bundles/angular2.dev.js"></script>
Run Code Online (Sandbox Code Playgroud)

当我终于意识到发生了什么时,我将导入声明还原为

import {Component, View} from 'angular2/core';
Run Code Online (Sandbox Code Playgroud)

这一切都奏效了.Angular2现在完全从上面的脚本标签加载,没有文件被额外的ajax调用加载.

有人可以解释是什么导致这个?我认为这是正常的行为,但我不明白导入的工作原理以及为什么指定更详细的路径会产生这样的差异.

import typescript angular

36
推荐指数
1
解决办法
5万
查看次数

Angular2 http.post执行两次

我遇到了一个奇怪的问题,Angular2的(RC1)Http服务执行两次http.post调用.我调试了我的应用程序,我知道这不是一个点击事件问题.所有通往核心服务呼叫的呼叫

public create(json: Object, params?: Object): Observable<T> {
    let body = JSON.stringify([json]);
    let headers = this.getHeaders();
    let options = new RequestOptions({ headers: headers });

    return this._http.post(this.createURL(this.getCreateURL(), [], params), body, options)
    .map(res => this.handleObjectResponse(res));
}
Run Code Online (Sandbox Code Playgroud)

运行一次.然后,当我开始跟踪问题时,我发现我的处理程序this.handleObjectResponse被执行了两次.所以我进一步钻研并达到@angular/http/src/backends/xhr_backend.ts了他们这样做的地方

constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
    this.request = req;
    this.response = new Observable<Response>((responseObserver: Observer<Response>) => {
        let _xhr: XMLHttpRequest = browserXHR.build();
        _xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
        // load event handler
        ...
        ..
Run Code Online (Sandbox Code Playgroud)

所以我打开一个断点this.request = req;,然后打开另一个断点let _xhr: XMLHttpRequest = browserXHR.build();,我发现我击中了第一个断点,但后来我从回调中击中了第二个断点两次. …

post http angular

33
推荐指数
1
解决办法
2万
查看次数

Angular2,*ngIf和本地模板变量

有人可以解释以下行为背后的原因:

假设我们有一个具有_model对象的Angular2组件.然后在模板中我们有这个

<form>
    <input type="text" class="form-control" required [(ngModel)]="_model.firstName" ngControl="test2"  #myInput >
    <br>Class: {{myInput?.className}}
</form>
Run Code Online (Sandbox Code Playgroud)

_model从一开始就可以在ngOnInit中从头开始创建.输入字段使用_model.firstName变量和行正确填充

_model

在模板中正确呈现以下内容

_model.

到现在为止还挺好.令我困惑的是,我添加*ngIf的那一刻,我将输入字段更改为

<input *ngIf="_model" type="text" class="form-control" required [(ngModel)]="_model.firstName" ngControl="test2"  #myInput >
Run Code Online (Sandbox Code Playgroud)

双花括号插值停止工作,因为ngOnInit()即使代码中没有其他内容发生变化,显然局部变量也不会被初始化,仍然会创建_model对象_model.firstName并且输入字段仍然正常工作.<br>Class: {{myInput?.className}}呈现的唯一的东西是

Class: form-control ng-untouched ng-pristine ng-invalid

有人可以解释发生了什么和/或指向我正确的文件吗?

提前致谢!

编辑:

这是一个能够显示问题的傻瓜

http://plnkr.co/edit/itNRpy5lc9PB837C7HdP?p=preview

创建错误报告https://github.com/angular/angular/issues/8087

interpolation angular

16
推荐指数
1
解决办法
2万
查看次数

Angular2 DI - 在同一构造函数中初始化多个不同的实例

我有一个Angular2 DI问题.假设我有一个TestService,我想在同一个组件中使用此服务的2个不同实例.如果我只是将一个提供程序添加到组件中,并将2个实例添加到构造函数中,那么我最终会使用相同的服务实例.例如:

TestService的

import {Injectable} from "@angular/core";

@Injectable()
export class TestService {

    public id: number = Math.random();

    public toString(): string {
        return "Id: " + this.id;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试组件

import {Component, Input, OnInit} from "@angular/core";
import {TestService} from "../../services/test.service";

@Component({
    providers: [TestService]
})
export class TestComponent implements OnInit {

    constructor(private _testService1: TestService, private _testService2: TestService) { };

    ngOnInit() {
        console.log("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", this._testService1.toString());
        console.log("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", this._testService2.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

结果在控制台中

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Id: 0.24242492129168425
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB Id: 0.24242492129168425
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我是否有办法使用Angular2的DI机制在同一组件中注入多个不同的服务实例,或者我应该删除这个特定情况的DI并使用手动构造函数手动创建我的实例?

提前致谢

dependency-injection typescript angular

8
推荐指数
2
解决办法
6993
查看次数

无法设置spring.datasource.type

我正在尝试在我的Spring启动服务器上设置c3p0.这是我的配置

spring.datasource.url=jdbc:mysql://url/db
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.test-on-borrow=true
#spring.datasource.test-while-idle=true
spring.datasource.validation-query=SELECT 1
#spring.datasource.time-between-eviction-runs-millis=10000
#spring.datasource.min-evictable-idle-time-millis=30000

spring.jpa.show-sql=true

spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
spring.jpa.properties.hibernate.connection.driver_class=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.connection.url=jdbc:mysql://url/db
spring.jpa.properties.hibernate.connection.username=username
spring.jpa.properties.hibernate.connection.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=true
#spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop


spring.jpa.properties.hibernate.c3p0.max_size=30
spring.jpa.properties.hibernate.c3p0.min_size=7
spring.jpa.properties.hibernate.c3p0.acquire_increment=1
spring.jpa.properties.hibernate.c3p0.idle_test_period=100
spring.jpa.properties.hibernate.c3p0.max_statements=0
spring.jpa.properties.hibernate.c3p0.max_idle_time=200
spring.jpa.properties.hibernate.c3p0.url=jdbc:mysql://url/db
spring.jpa.properties.hibernate.c3p0.username=username
spring.jpa.properties.hibernate.c3p0.password=password
spring.jpa.properties.hibernate.c3p0.driverClassName=com.mysql.jdbc.Driver
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法弄清楚如何告诉spring.datasource使用

com.mchange.v2.c3p0.ComboPooledDataSource
Run Code Online (Sandbox Code Playgroud)

我看到的所有XML定义都使用了类似的东西

<bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource">
Run Code Online (Sandbox Code Playgroud)

是否无法在application.properties中设置数据源类型/类?

根据这个

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

spring.datasource.type= # fully qualified name of the connection pool implementation to use
Run Code Online (Sandbox Code Playgroud)

但根据这个

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

(以及我的STS).type选项不存在.这是一个错误还是我应该以不同的方式使用它?

非常感谢您的帮助!

干杯!

spring datasource c3p0 spring-boot

4
推荐指数
1
解决办法
1万
查看次数

Java 8函数<String,Void> vs Consumer <String>

我不能为我的生活找到以下的解释:

public static void takesAFunction(Function<String, Void> func) {
    func.apply("Hi I'm running a function");
}

public static void takesAConsumer(Consumer<String> func) {
    func.accept("Hi I'm running a consumer");
}

public static void main(String[] args) throws Exception {
    takesAFunction((String str) -> { System.out.println(str); });
    takesAConsumer((String str) -> { System.out.println(str); });
}
Run Code Online (Sandbox Code Playgroud)

我正在使用JDK 1.8.0_66和该行

takesAFunction((String str) -> { System.out.println(str); });
Run Code Online (Sandbox Code Playgroud)

被标记为错误

The method takesAFunction(Function<String,Void>) in the type MyClass 
is not applicable for the arguments ((String str) -> {})
Run Code Online (Sandbox Code Playgroud)

我不明白是怎么回事

Function<String, Void> 
Run Code Online (Sandbox Code Playgroud)

不同于

Consumer<String>
Run Code Online (Sandbox Code Playgroud)

当两者都不返回并且都接受单个String参数时.

有人可以解释一下它是否会被杀死. …

java lambda callable runnable java-8

4
推荐指数
1
解决办法
1125
查看次数

Angular2路由 - 使用LocationStrategy添加标签不起作用?

我正在关注Angular2文档中的简单快速启动应用程序,我正在使用spring后端来运行它.我的问题是,角度路由器从URL中删除了主题标签,所以现在应该example.com/#/dashboard是这样example.com/dashboard.

我正在使用LocationStrategyStackOverflow上的一堆帖子中指定的方法.以下是我的简单示例:

File: main.ts

///<reference path="../node_modules/angular2/typings/browser.d.ts"/>

import {bootstrap} from 'angular2/platform/browser'
import {provide} from 'angular2/core';
import {LocationStrategy, HashLocationStrategy} from 'angular2/router';

import {TestComponent} from './simple/test.component'

bootstrap(
    TestComponent, 
    [
        provide(LocationStrategy, { useClass: HashLocationStrategy })
    ]
);
Run Code Online (Sandbox Code Playgroud)

File: test.component.ts

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';

@Component({
    selector: 'test1',
    template: "<h1>This is test1 component</h1>"
})
export class Test1 { };

@Component({
    selector: 'test2',
    template: "<h1>This is test2 component</h1>"
})
export class Test2 { }; …
Run Code Online (Sandbox Code Playgroud)

url hashtag angular2-routing angular

4
推荐指数
1
解决办法
5045
查看次数