小编Jam*_*mes的帖子

将值传递给组件时是否有可选的 input() 条件之类的东西?

标题可能有误导性,我详细解释一下。

假设我们有一个集合并使用ngfor它在 html 中绑定它们。然后,对于每个集合,根据条件,我们将值传递给子组件。例如:

<ng-template ngFor let-person="$implicit" [ngForOf]="peopleAsPreviousCurrentAndNextTriplets" let-i=index let-last=last>
  <app-card 
  [decrement]="(2 - i)" 
  [defaultDiff]="(+30) * (2 - i)" 
  [prevCard]="person.previous" 
  [currentCard]="person.current" 
  [nextCard]="person.next"
  ></app-card>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我们正在经过[prevCard], [currentCard],[nextCard]传递给集合中每个元素的组件。

然而我不想要这个。

我只想通过[prevCard],,[currentCard][nextCard]last如果没有last那么我只想通过[currentCard]

我怎样才能在这里做到这一点?

collections typescript ngfor angular

6
推荐指数
1
解决办法
5769
查看次数

两个相似的函数如何在Haskell中具有不同的多态类型?

我几乎是Haskell的新手,所以如果我缺少关键概念,请指出.

让我们说我们有这两个功能:

fact n
     | n == 0 = 1
     | n > 0  = n * (fact (n - 1))
Run Code Online (Sandbox Code Playgroud)

多态类型fact(Eq t, Num t) => t -> t因为n在if条件中使用而n必须是有效类型才能进行==检查.因此t必须是a,Number并且t可以是类约束中的任何类型Eq t

fib n
    | n == 1 = 1
    | n == 2 = 1
    | n > 2  = fib (n - 1) + fib (n - 2)
Run Code Online (Sandbox Code Playgroud)

那为什么多态类型fib(Eq a, Num a, …

polymorphism haskell types function fibonacci

4
推荐指数
2
解决办法
75
查看次数

如何从模板绑定中的角度4访问getter/setter访问器?

假设我有以下getter/setter方法

get next() {
  console.log(this.people[this._index], this._index);
  return this.people[this._index];
}

set next(i: any) {
  this._index = (+i) + 1;
  this._index = (+i) % this.people.length;
}
Run Code Online (Sandbox Code Playgroud)

我想用以下方式调用它:

<ng-template ngFor let-person="$implicit" [ngForOf]="people" let-i=index let-last=last>
  <app-card [cardItem]="people[i]" [nextCard]="next(i)"></app-card>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

PS:把它想象成圆形阵列.我在哪里需要prev,current和next items.

但是我得到以下错误

Angular:会员'下一个'不可赎回

这是为什么?最新解决方案是什么?

谢谢

编辑

谢谢你们的帮助和解释.在你的帮助下,我设法让它发挥作用:

<app-card [currentCard]="people[i]" [nextCard]="people[i === people.length - 1 ? 0: i + 1]" [prevCard]="i == 0 ? people[people.length - 1] : people[i - 1]"></app-card>
Run Code Online (Sandbox Code Playgroud)

所以它几乎是圆形阵列.让我们假设我们有以下内容:

people["James Dan", "Aluan Haddad", "Jota Toledo"]

条件很少:

  1. 如果我站在数组的开头(即index = 0) - …

templatebinding getter-setter typescript angular

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

在Polymer 2.0中的HTMLElement.onclick中未定义未捕获的ReferenceError抽屉?

我是聚合物2.0的新手,我正试图解决我似乎无法理解的某些错误.

其中一个是这样的:

Uncaught ReferenceError: drawer is not defined at HTMLElement.onclick

所以为了解释我的问题,我试图使用app-drawer哪个将在点击事件中从左向右滑动.但click事件会返回上述错误.如何确定抽屉的定义?(顺便说一句,我正在按照app-layout部分主网站上提供的代码片段进行操作. - https://www.webcomponents.org/element/PolymerElements/app-layout).

这是我的代码:

<app-header reveals>
  <app-toolbar>
    <paper-icon-button icon="menu" onclick="drawer.toggle()"></paper-icon-button>
    <div main-title>My app</div>
    <paper-icon-button icon="delete"></paper-icon-button>
    <paper-icon-button icon="search"></paper-icon-button>
    <paper-icon-button icon="close"></paper-icon-button>
    <paper-progress value="10" indeterminate bottom-item></paper-progress>
  </app-toolbar>
</app-header>
<app-drawer id="drawer" swipe-open></app-drawer>
Run Code Online (Sandbox Code Playgroud)

这在网站上是完全相同的,所以为什么它适用于他们,但不适用于我.

单击菜单图标时会出现问题.

请帮忙.

谢谢

html onclicklistener polymer custom-element polymer-2.x

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

Haskell中的多态类型

我遇到过这个功能

iter p f x = if (p x) then x else (iter p f (f x))
Run Code Online (Sandbox Code Playgroud)

我以为我会自己定义多态类型来理解这个概念.

我的想法如下:

该函数有3个参数,所以我们有 t1 -> t2 -> t3 -> T

  1. p正在if条件中使用bool,因此必须返回at1 = a -> Bool

  2. f也是p因为它在else块中作为参数传递的类型相同t2 = a -> Bool

  3. x正在if条件中使用,因此它必须返回bool t1 = a -> Bool

但是当我检查ghci中的类型时,他们给我的类型是

iter :: (t -> Bool) -> (t -> t) -> t -> t

有人可以解释一下这背后的原因.

谢谢

polymorphism haskell types function polymorphic-functions

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

多态类型的函数作为haskell中的参数?

我试图定义以下函数的多态类型:

flip f x y = f y x
Run Code Online (Sandbox Code Playgroud)

我的想法如下:

  1. 第一个参数flip,f取两个参数(t1 -> t2 -> t3)

  2. 由于函数内部的参数flip,x属于第二个参数类型.t1t1f

  3. 第三个参数flip,由于函数内部的参数y而属于类型.t3t3f

  4. 我不知道整体回报的多态类型.

但是当我检查ghci中的类型时,我得到:

flip :: (t2 -> t1 -> t) -> t1 -> t2 -> t
Run Code Online (Sandbox Code Playgroud)

有人可以请帮助通过这个例子是什么发生在这里?

谢谢

polymorphism haskell types function polymorphic-functions

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

如何检查一个元素是否存在于haskell的列表中?

我知道之前有人问过这个问题,但答案偏离了主要问题。

这是一个检查元素是否存在于 Haskell 中的方法

elem’ x (y : ys) = if x == y then True else elem’ x ys

我感到困惑的是,在 Haskell 中,(y : ys)这将 y 添加到 ys,那么此函数如何真正检查元素是否存在?因为我在这里没有看到任何循环,除了将相同的递归调用传递yys.

请赐教。

recursion haskell if-statement function list

-1
推荐指数
1
解决办法
7398
查看次数