小编Nir*_*rtz的帖子

Angular2禁用按钮

我知道在angular2中我可以禁用具有该[disable]属性的按钮 ,例如:

<button [disabled]="!isValid" (click)="onConfirm()">Confirm</button>
Run Code Online (Sandbox Code Playgroud)

但我可以做到这一点使用[ngClass][ngStyle]?像这样:

<button [ngStyle]="{disabled : !isValid}" (click)="onConfirm()">Confirm</button>
Run Code Online (Sandbox Code Playgroud)

谢谢.

html angular2-forms angular2-template angular

106
推荐指数
5
解决办法
19万
查看次数

async和await是单线程真的吗?

我创建了以下代码:

using System;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
       {
         Console.WriteLine("M Start");
         MyMethodAsync();
         Console.WriteLine("M end");
         Console.Read();
       }

     static async Task MyMethodAsync()
     {
        await Task.Yield();
        Task<int> longRunningTask = LongRunningOperationAsync();
        Console.WriteLine("M3");
        //and now we call await on the task 
        int result = await longRunningTask;
        //use the result 
        Console.WriteLine(result);
     }

       static async Task<int> LongRunningOperationAsync()  
      {
        await Task.Delay(1000);
        return 1;
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

输出:

M Start
M end
M3
1
Run Code Online (Sandbox Code Playgroud)

哪个好,但是当我查看Thread Profiler时,它显示了这个: 在此输入图像描述 然后这个: 在此输入图像描述 然后这个: 在此输入图像描述

所以看起来我生成线程,但是从msdn说:

从Async和Await的异步编程:线程

async和await关键字不会导致创建其他线程.异步方法不需要多线程,因为异步方法不能在自己的线程上运行.该方法在当前同步上下文上运行,并仅在方法处于活动状态时在线程上使用时间.您可以使用Task.Run将CPU绑定的工作移动到后台线程,但后台线程无助于正在等待结果可用的进程.

我错过了什么或者不理解什么?谢谢.

.net c# multithreading task async-await

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

错误_renderer.setElementStyle"无法在[null]中设置未定义的属性'background-color'"

我在angular2udemy课程中学习angular2,老师写了一个突出显示html元素的指令.

我试图做休闲,但对我来说_renderer.setElementStyle抛出异常.

EXCEPTION:TypeError:无法在[null]中设置undefined的属性'background-color'

指令:

import {Directive, ElementRef, Renderer, OnInit} from 'angular2/core';

@Directive({
    selector: '[highlight-directive]'
})

export class HighlightDirective implements OnInit{
    private _defaultColor= 'green';

    constructor(private _elmRef: ElementRef, private _renderer: Renderer) {}

    ngOnInit(): any {
        this._renderer.setElementStyle(this._elmRef, "background-color", this._defaultColor);
        //this._elmRef.nativeElement.style.backgroundColor = this._defaultColor; //this way works fine.
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用该指令的模板:

template: `
    <div highlight-directive>
        Highlight me
    </div>
    <br>
    <div highlight-directive>
        2 Highlight me 2
    </div>
`,
Run Code Online (Sandbox Code Playgroud)

教师工作区: 在此输入图像描述

任何人都能找到我做错的事吗?

谢谢.

html angular2-forms angular2-directives angular2-template angular

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

填充现有对象时在添加项目之前清除集合

我有一个对象,它有几个使用公共 getter 和私有 setter 定义的集合属性,在这种情况下,JsonConvert.PopulateObject将反序列化的项目添加到这些集合中,而现有项目保持不变。

在反序列化之前清除此类成员集合时,我需要一种行为。

我尝试在标记有该[OnDeserializing]属性的方法中手动清除集合。

这种方法的问题在于,即使 JSON 字符串中不存在集合属性,它仍然会清除集合。

当只有那些实际上在 JSON 字符串中定义的集合被清除时,我需要一种方法。那些未定义的应该保持不变。

谢谢

c# json json.net

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

TypeScript构造函数

我试图创建一个具有两个构造函数的类,并发现TypeScript不允许这样,但它允许重载构造函数,我试过它并得到一个错误:

构建:重载签名与功能实现不兼容.

我的代码:

interface IShoppingListItem {
    name: string;
    amount: number;
}

export class ShoppingListItem implements IShoppingListItem{
    name: string;
    amount: number;

    constructor(item: IShoppingListItem);
    constructor(name: string, amount: number) {
        this.name = name;
        this.amount = amount;
    }

    copy() {
        //return new this.constructor(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

我有两个问题,第一个问题,为什么我不能重载构造函数,我想我做错了什么.

但我的第二个问题,以及更多的交互是,我知道我的构造函数获取可选值.我可以(不使用方法中的代码!),在我的构造函数上创建一个条件,可以验证两个给定值中的一个必须存在,而在签名引导中是可选的,如下所示:

constructor(item?: IShoppingListItem, name?: string, amount?: number) { 
//make a condition that item or name and amount must exist
    this.name = name;
    this.amount = amount;
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

typescript typescript1.8

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

angular2自定义指令输入语法

我创建一个自定义指令并将选择器值设置为" [unless-directive] ".

该指令获取一个布尔值并使用它来更改视图,如下所示:

import {Directive, TemplateRef, ViewContainerRef} from 'angular2/core';

@Directive({
    selector: '[unless-directive]',
    inputs: ['givenBoolean : myDirectiveFunction']
})

export class UnlessDirective {
    private _templateRef: TemplateRef;
    private _viewContainerRef: ViewContainerRef;


    constructor(_templateRef: TemplateRef, _viewContainerRef: ViewContainerRef) {
        this._templateRef = _templateRef;
        this._viewContainerRef = _viewContainerRef;
    }

    set myDirectiveFunction(condition: boolean) {
        !condition ? this._viewContainerRef.createEmbeddedView(this._templateRef)
            : this._viewContainerRef.clear();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的模板中,我试图传递这样的条件:

<div name="customDirective">
    <h2>Custom Directive</h2>
    <div>
        Enter true or false:
        <br/>
        <input type="text" #condition (keyup)="0"/>
        <div *unless-directive [givenBoolean]="condition.value != 'false'">
            Only shown if 'false' wad enterded!
        </div> …
Run Code Online (Sandbox Code Playgroud)

html angular2-forms angular2-directives angular2-template angular

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

Swift, macos - 我可以检测或使其他应用程序处于前台吗?

我正在用 Swift 在 macos 上创建一个应用程序,

该应用程序需要打开 Safari,我需要 Safari 应用程序始终位于前台。

可以做到吗?

macos swift swift4

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