我正在学习关于 Angular4 ngStyle 的教程。
我有以下代码:
应用程序组件.html
<button
[ngStyle]="{
'backgroundColor': canSave ? 'blue': 'gray',
'color': canSave ? 'white': 'black',
'fontWeight': canSave ? 'bold': 'normal'
}"
>
Save
</button>
Run Code Online (Sandbox Code Playgroud)
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
canSave = 'false';
}
Run Code Online (Sandbox Code Playgroud)
无论 canSave 是真还是假,我都明白:
控制台看起来像这样:
我不明白为什么这不起作用!三元运算符条件似乎没有任何区别。我的语法错了吗?我直接从教程中复制了它,它似乎在其他情况下也能工作?
我正在阅读 C++ 入门第 5 版,第 10 章(lambdas 表达式),这是一个旨在用绝对值替换向量中的负值的程序。
transform(vi.begin(), vi.end(), vi.begin(),
[](int i) { if (i < 0) return -i; else return i; });
Run Code Online (Sandbox Code Playgroud)
作者说:
这段代码无法编译,因为 lambda 将返回类型推断为 as
void但我们返回了一个值,为了解决这个问题,我们必须使用尾随返回类型。
但是当我在 Windows 上用 GNU GCC Compiler 编译这段代码时,它运行良好。
作者还说:
这个版本编译是因为我们不需要指定返回类型,因为可以从条件运算符的类型推断出该类型。
transform(vi.begin(), vi.end(), vi.begin(),
[](int i) { return i < 0 ? -i : i; });
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:
我试图了解三元运算符在 C++ 中的工作原理。
我希望看到两个打印语句的输出相同,但第二个打印语句输出 49。
为什么是这样?
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "First character " << '1' << endl;
cout << "Second character " << (test ? 3 : '1') << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
第一个字符 1
第二个字符 49
我不希望在 else 条件下第二个元素为 null .. 我只是不想要第二个元素。如何解决这个问题?
var template = "";
var myArray = [
{ element : "header" },
template === 'foo' ? { element : "sidebar" } : null,
{ element : "footer" }
]
console.log(myArray)Run Code Online (Sandbox Code Playgroud)
我已经使用了 helm 三元运算符,我认为它非常需要,但它似乎只能返回布尔值。我可以让它返回任意值而不仅仅是 true 或 false 吗?
这按预期工作
共享VPC:{{ true | (eq .Values.global.hyperscaler "gcp") false | 引用 }}
我很想能够做这样的事情
reclaimJobHours: {{ ternary (eq .Values.global.hyperscaler "aws") "36" "34" }}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用条件语句覆盖 toString。但是我收到一个错误,指出类型不匹配。它应该是 String 类型,现在它是 Any 类型。noGoals、Tied 是返回 true 或 false 的函数。
override def toString = {
if (this.noGoals)
s"${this.home} vs. ${this.away} at ${this.location}: no goals"
else if (this.Tied)
s"${this.home} vs. ${this.away} at ${this.location}: tied"
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(void){
int n = 0, y = 1;
y == 1 ? n = 0 : n = 1;
if (n)
printf("YES");
else
printf("NO");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么带有三元运算符的行会lvalue出错。我对什么有一个非常抽象的概念lvalue。让我给出我的抽象,如果我错了,请纠正我。lvalue通常是地址,或者我们可以说我们存储常量值的变量,变量或常量的值是 a rvalue。但我不明白为什么lvalue三元运算符的赋值部分会出现错误,其中指出n = 0 : n = 1. 如果我能够正确理解我的代码出了什么问题,那将会非常有帮助。
这是我想要的行为......
result = []
for element in iterable
if(condition)
push!(result, element)
else
continue
end
end
Run Code Online (Sandbox Code Playgroud)
但是当在 Julia 中使用带有三元运算符的数组推导式时,我不确定除了什么都不使用之外类似的“继续”会是什么。这会导致不想要的 Vector{Union{Nothing, ...}}。
result = [(condition ? element : nothing) for element in iterable]
Run Code Online (Sandbox Code Playgroud)
例如:
vec1 = []
for v in -1:1
if(v==0)
push!(vec1,v)
else
continue
end
end
Run Code Online (Sandbox Code Playgroud)
返回
julia> vec1
1-element Vector{Any}:
0
Run Code Online (Sandbox Code Playgroud)
同时
vec2 = [(v==0 ? v : nothing) for v in -1:1]
Run Code Online (Sandbox Code Playgroud)
返回
julia> vec2 = [(v==0 ? v : nothing) for v in -1:1]
3-element Vector{Union{Nothing, Int64}}:
nothing …Run Code Online (Sandbox Code Playgroud) 我是 Perl 开发的新手,我想检查是否允许在 push 语句中使用条件运算符。像这样的东西
push(@myMap, {
key1 => value1,
key2 => value2,
(condition)?{
key3 => value3,
}:(),
key4 => value4
});
Run Code Online (Sandbox Code Playgroud)
基本上我只想根据条件向地图添加一个值
以下表达式:-
int main()
{
int x=2, y=9;
cout << ( 1 ? ++x, ++y : --x, --y);
}
Run Code Online (Sandbox Code Playgroud)
给出以下输出:-
9
Run Code Online (Sandbox Code Playgroud)
根据我的理解,它应该返回 ++y,它应该是 10。出了什么问题?
c++ conditional-operator operator-precedence prefix-operator
c++ ×3
angular ×1
c ×1
ecmascript-6 ×1
if-statement ×1
javascript ×1
julia ×1
lambda ×1
lvalue ×1
perl ×1
scala ×1
vector ×1