小编pix*_*its的帖子

在Angular 2中等效$ compile

我想手动编译一些包含HTML的指令.$compileAngular 2中的等价物是什么?

例如,在Angular 1中,我可以动态编译HTML片段并将其附加到DOM:

var e = angular.element('<div directive></div>');
element.append(e);
$compile(e)($scope);
Run Code Online (Sandbox Code Playgroud)

version-compatibility typescript angular2-compiler angular

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

发出单个值

我想发出一个值.

根据这篇文章,以下内容应该是有效的:

var requestStream = Rx.Observable.just('https://api.github.com/users');
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.just没有定义.

它被弃用了吗?有什么东西我可以用吗?

javascript rxjs angular

25
推荐指数
3
解决办法
2万
查看次数

@ContentChildren没有被填充

我有三个组件:App,Parent和Child:

应用组件

@Component({
    selector: 'app',
    directives: [Parent,Child],
    template: '<parent><child>hi</child><child>there</child></parent>'
})
export class AppComponent {
    constructor() {
    }
}
Run Code Online (Sandbox Code Playgroud)

父组件

@Component({
    selector: 'parent',
    template: '<div><h1>Parent</h1><ng-content></ng-content></div>'
})
export class Parent {
    @ContentChildren(Child) children: QueryList<Child>;
    ngAfterContentInit() {
        console.log(this.children);
    }
}
Run Code Online (Sandbox Code Playgroud)

子组件

@Component({
    selector: 'child',
    template: '<div><h1>Child</h1></div>'
})
export class Child {
}
Run Code Online (Sandbox Code Playgroud)

正如您在Parent组件中看到的那样,我尝试使用类型作为选择器@ContentChildren来获取Child组件列表Child.但是,这似乎不起作用 - 内容子列表始终未定义.

在该ngAfterContentInit()方法中,我希望可以填充内容子项.

我错过了什么吗?

[更新]

事实证明,当所有三个组件都在同一个文件中时,问题就存在了(参见我输出内容子项的控制台调试窗口):

Plnkr问题演示

如果它们位于单独的文件中,则问题就会消失:

Plnkr演示工作

通常,我只会将所有组件放在同一个文件中以供学习.但它让我很好奇.有谁知道为什么行为不同?

angular

17
推荐指数
2
解决办法
9946
查看次数

Angular2如何在Plain JS中定义输入属性

我想使用普通JS(而不是typescript)为我的组件指定一个Input属性.

我能找到的唯一文件就是这个(来自Angular2 备忘单):

ng.core.Input(myProperty, myComponent); 
//Declares an input property that we can 
//update via property binding (e.g. <my-cmp [my-property]="someExpression">).
Run Code Online (Sandbox Code Playgroud)

我在我的组件构造函数中尝试过这个:

.Class({
   constructor: function () {    
       ng.core.Input(this.name, this);        
   }
});      
Run Code Online (Sandbox Code Playgroud)

但是,它不起作用(也没有报告错误).

我究竟做错了什么?

angular

14
推荐指数
1
解决办法
1817
查看次数

gulp-jasmine窗口未定义错误

我正在使用gulp/gulp-jasmine/angular来运行我的单元测试.但是,在运行Gulp目标时遇到以下错误:

C:\Projects\website2>gulp test
[01:53:10] Using gulpfile C:\Projects\website2\gulpfile.js
[01:53:10] Starting 'test'...
[01:53:11] Version: webpack 1.4.13
         Asset     Size  Chunks             Chunk Names
test.bundle.js  1051728       0  [emitted]  test
F

Failures:
1) Exception loading: C:\Projects\website2\scripts\dist\test.bundle.js Error
1.1) ReferenceError: window is not defined

1 spec, 1 failure
Finished in 0.015 seconds
[01:53:11] 'test' errored after 916 ms
[01:53:11] Error in plugin 'gulp-jasmine'
Message:
    Tests failed
Run Code Online (Sandbox Code Playgroud)

我相信gulp-jasmine使用PhantomJS(没有触发浏览器窗口).有人可以帮我解决我做错的事吗?我缺少配置设置吗?

这是我的gulpfile.js:

var gulp = require('gulp');
var path = require('path');
var webpack = require('gulp-webpack');
var webpackConfig = require('./webpack.config');
var testWebpackConfig = …
Run Code Online (Sandbox Code Playgroud)

jasmine angularjs gulp gulp-jasmine

12
推荐指数
2
解决办法
9205
查看次数

将ngModel绑定到选择控件的模型

在Angular 1.x中,您可以将ngModel绑定到select控件的模型:

<select ng-model="selectedPerson" 
   ng-options="person as person.name for person in people">
</select>
Run Code Online (Sandbox Code Playgroud)

选择选项后,selectedPerson模型将指向person用户选择的模型.

有没有办法在Angular2中做同样的事情?

我试过以下没有运气:

<select [(ngModel)] = "selectedPerson"> 
     <option *ngFor="#person of people"> {{ person.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

<select [(ngModel)] = "selectedPerson"> 
     <option *ngFor="#person of people" [value]="person"> {{ person.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

在第一次尝试中,selectedPerson模型引用person.name而不是person对象.在第二次尝试中,它引用了一个似乎不是JSON对象的对象.

我有什么想法我做错了吗?这甚至可能吗?

angular

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

.NET Core Runtime是否与以前的版本向后兼容?

如果我已经安装了.NET Core Runtime的最新版本(截至目前,该版本是2.2.3):

https://dotnet.microsoft.com/download/dotnet-core/2.2

该安装是否与.NET Core的先前版本向后兼容?

例如,上述安装是否与针对以下应用的应用兼容:

  • netcoreapp1.0
  • netcoreapp2.0
  • netcoreapp2.1
  • netcoreapp2.2

我意识到.NET运行时可以并排安装。但是,这不能回答我的问题。2.2版的安装是否向后兼容以前的版本,从而不需要额外的运行时?

.net-core asp.net-core-1.0 asp.net-core-2.1

9
推荐指数
1
解决办法
2430
查看次数

AngularJS:如何从ng-click停止事件传播?

我有指令做这样的事情:

app.directive('custom', function(){
    return {
        restrict:'A',
        link: function(scope, element){
            element.bind('click', function(){
                alert('want to prevent this');
            });

        }
    }
});
Run Code Online (Sandbox Code Playgroud)

是的,有必要为这种情况做jQuery-way绑定.

现在,如果满足某些条件,我想停止此事件(单击)传播.

试图做:

  $event.stopPropagation();
  $event.preventDefault();
Run Code Online (Sandbox Code Playgroud)

但它没有帮助.

这里小提琴 - 例如http://jsfiddle.net/STEVER/5bfkbh7u/

javascript event-propagation angularjs angularjs-directive

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

当静态forRoot具有参数时,在AOT构建期间FeatureModule失败

我正在使用Angular@5.1.0遇到AOT构建问题.

错误是:

ERROR in Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'FeatureModule' was called.
Run Code Online (Sandbox Code Playgroud)

feature.module.ts

@NgModule({
    imports: [
        BrowserModule,
        RouterModule.forRoot([])
    ],
    declarations: [
        ...    
    ],
    exports: [
        ...      
    ]
})
export class FeatureModule{
    static forRoot(items:any[]): ModuleWithProviders {
        const routes:Routes = items.map(t=> {
            return { path: t.name, component: t.component };
        });

        return {
            ngModule: FeatureModule, 
            providers: [
                provideRoutes(routes)
            ]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这在非aot构建中成功编译.这似乎只是AOT构建的一个问题.

为什么会发生这种错误?

aot angular

8
推荐指数
1
解决办法
1640
查看次数

如何使用角度重置表单

这是我的代码:

function CustomersController ($scope, $http) {
    $http.get('/ci_angular/api/customers/').success(function(customers){
        $scope.customers = customers;
    });

    $scope.addCustomer = function(){
        var customer = {
            customerName: $scope.customerNameText,
            email: $scope.emailText,
            address: $scope.addressText,
            city: $scope.cityText,
            state: $scope.stateText,
            postalCode: $scope.postalCodeText,
            country: $scope.countryText,
        };

        $scope.customers.push(customer);

        // update 1

        $http.post('/ci_angular/api/customers/customer', customer);

        $scope.emptyCustomers= {};

        $scope.reset = function() {
            $scope.customers = angular.copy($scope.emptyCustomers);
        };

        // update 2

        $http.post('/ci_angular/api/customers/customer', customer)
            .success(function() {           
                 $scope.emptyCustomers= {};

                 $scope.customerForm.$setPristine();
            }
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

在成功后,我想重置所有表单字段,但它不起作用,我的表单名称是customerForm,有什么我错过了吗?提前致谢

reset angularjs

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

如何使用 Angular.js 动态生成 DOM?

我正在开始使用 Angular.js,我想知道如何按照以下方式做一些事情(伪代码):

<li ng-repeat="item in items">
    <# if(item.dataType == "string") { #>
        <input type="text" />
    <# } else if(...) { #>
        <input type="password" />
    <# } #>
</li>
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码不是有棱角的,而且我知道为了简单的处理,我可以使用条件ng-hideng-show类似的东西。但对于复杂的行为,如果我必须执行各种数据检查和业务逻辑,我如何使用 Angular.js 动态生成 DOM 元素?

javascript angularjs

3
推荐指数
1
解决办法
1249
查看次数

如何在插值字符串中使用变量?

我正在尝试在插值字符串中使用变量,但运气不佳。我该怎么做?

var name = "mike";
var desc = "hello world {name}";
var t = $"{ desc }";
Console.WriteLine(t); // PRINTS: hello world {name}
Run Code Online (Sandbox Code Playgroud)

这是我要实现的目标:

Console.WriteLine(t); // PRINTS: hello world mike
Run Code Online (Sandbox Code Playgroud)

这可能吗?

例如,假设我有一个方法:

public string FormatString(string s) {
      var Now = DateTime.Now;
      return $s;
}
Run Code Online (Sandbox Code Playgroud)

用法:

Console.WriteLine(FormatString("The time is {Now}"));
Run Code Online (Sandbox Code Playgroud)

c# roslyn .net-core

0
推荐指数
1
解决办法
1097
查看次数