是否有一种转储或检查表达式结果的好方法?有时当我这样做
{{some_expression}}
Run Code Online (Sandbox Code Playgroud)
,没有任何内容显示在该表达式的结果应显示的页面上.如何确定表达式是返回a null,an undefined还是空字符串''?
如果它是一个普通的对象,就像这样,它会显示一个很好的程序员友好的对象表示,这很棒:
{{ {'a': 1} }}
Run Code Online (Sandbox Code Playgroud)
但是如果你试图检查一个计算结果为null,undefined或''的表达式,它们彼此之间就无法区分了!
{{null}}
{{undefined}}
{{''}}
Run Code Online (Sandbox Code Playgroud)
那你怎么知道它是哪一个?
我试过用JSON.stringify:
{{ JSON.stringify(null) }}
Run Code Online (Sandbox Code Playgroud)
但JSON似乎从Angular表达式中不可用,因为它是一个方法,window而不是范围的属性(请参阅有关从窗口访问方法的相关问题).
我试过用typeof:
typeof {}: {{ typeof {'a': 1} }}
Run Code Online (Sandbox Code Playgroud)
但它会导致错误:
Error: [$parse:syntax] Syntax Error: Token '{' is an unexpected token at column 9 of the expression [ typeof {'a': 1} ] starting at [{'a': 1} ].
Run Code Online (Sandbox Code Playgroud)
那么如何才能使用JSON.stringify(......或console.log)之类的东西将值转储到模板中?
一般来说,除了试验和错误之外,还有一种调试Angular表达式的好方法吗?由于Angular 表达式是如此"宽容",它们似乎不会引起错误; 他们只是默默地吞下错误并返回 …
我该怎么做这样的事情:
{{ $use_ssl := (ne $.Env.CERT_NAME "") }}
Run Code Online (Sandbox Code Playgroud)
哪里$.Env.CERT_NAME可能是零/未定义.如果它是nil,则会出现此错误:
at <ne $.Env.CERT_NAME "">: error calling ne: invalid type for comparison
Run Code Online (Sandbox Code Playgroud)
注意:我无法控制传入Go模板的对象,因此必须在模板本身内完全解决这个问题.
我试图通过首先检查它是否为非空来解决:
{{ $use_ssl := ( ($.Env.CERT_NAME) && (ne $.Env.CERT_NAME "") ) }}
Run Code Online (Sandbox Code Playgroud)
但它给出了这个错误:
unexpected "&" in operand
Run Code Online (Sandbox Code Playgroud)
所以我切换到这个,这在语法上是允许的:
{{ $use_ssl := (and ($.Env.CERT_NAME) (ne $.Env.CERT_NAME "") ) }}
Run Code Online (Sandbox Code Playgroud)
但后来我失去了我对操作员的短路评估,我&&又回到了这个错误:
at <ne $.Env.CERT_NAME ...>: error calling ne: invalid type for comparison
Run Code Online (Sandbox Code Playgroud)
好吧,我想,如果我不能用一个漂亮的单行,并且Go没有三元运算符,那很好,我只会用惯用的Go方式来做,这显然是if/else.
{{ if $.Env.CERT_NAME }} …Run Code Online (Sandbox Code Playgroud) 我喜欢%r<…>定界符,因为它使查找正则表达式的开始和结束变得非常容易,而且我也不必转义任何内容/。但是,它们似乎有其他定界符没有的无法克服的限制?
可以想象的所有其他定界符都可以正常工作:
/(?<!foo)/
%r{(?<!foo)}
%r[(?<!foo)]
%r|(?<!foo)|
%r/(?<!foo)/
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这样做时:
%r<(?<!foo)>
Run Code Online (Sandbox Code Playgroud)
它给出以下语法错误:
unterminated regexp meets end of file
Run Code Online (Sandbox Code Playgroud)
好吧,这可能不一样,它不是一个平衡的对,但是你怎么逃避它,使得它不喜欢吗?
是否需要逃脱?
任何单个非字母数字字符都可以用作分隔符
%[including these], %?or these?, %~or even these things~。通过使用这种表示法,通常的字符串定界符“和”可以出现在未转义的字符串中,但是当然您必须转义选择的新定界符。
实际上,在以下示例中需要转义:
%r!(?<\!foo)!
%r?(\?<!foo)?
Run Code Online (Sandbox Code Playgroud)
但是,如果那是唯一的问题,那么我应该能够像这样逃脱它并使它起作用:
%r<(?\<!foo)>
Run Code Online (Sandbox Code Playgroud)
但这会产生此错误:
undefined group option: /(?\<!foo)/
Run Code Online (Sandbox Code Playgroud)
因此,也许逃避是没有必要/允许?wikibooks.org确实将以下情况%<pointy brackets>列为例外之一:
但是,如果使用
%(parentheses), %[square brackets], %{curly brackets}或%<pointy brackets>作为分隔符,然后那些相同的分隔符可以出现转义,因为它们中的字符串,只要在平衡 对
平衡对有问题吗?
只要您在Regexp中执行需要平衡对的操作即可,例如...
%r{(?<!foo{1})} # repetition quantifier
%r[(?<![foo])] # character class …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个指令,列出错误对象({phone: ["is required"]}中包含的所有错误,但仅当对象是非空的时.(当有"以下错误......"时没有意义)没有.)
我想通过测试来检查对象是否为空Object.keys(errors).length. 问题是我无法弄清楚如何从我的指令模板访问Object.keys. 这可能吗?
由于Angular表达式是在" 而不是在上下文"的上下文中"评估"(使用$parse,而不是eval()),因此我们无法访问指令模板中的内容,因为它不是作用域的属性.(文件:表达式)scopewindowObjectObject
到目前为止有道理.它继续说"与JavaScript不同,其中名称默认为全局window属性,Angular表达式必须$window显式地用于引用全局window对象.例如,如果要调用alert()表达式,则必须使用$window.alert().
但Object即使我这样做,我似乎无法访问$window.Object.我错过了什么?
这是我正在调试的指令的代码(这里是一个jsfiddle):
app.js.coffee:
…
.directive 'displayErrorsAllKeys', ->
{
restrict: 'A',
scope: {
errors: '='
debug: '@debug'
}
templateUrl: 'templates/display-errors-all-keys'
}
.run(['$rootScope', ($rootScope) ->
$rootScope.nonEmpty = (object) ->
!! Object.keys(object).length
])
.controller('TestDisplayErrorsAllKeys',
['$scope',
( $scope ) ->
$scope.other_errors = {} …Run Code Online (Sandbox Code Playgroud) 我正在尝试更改一个函数 \xe2\x80\x94 ,该函数当前仅接受 URL(路径)字符串\xe2\x80\x94 以使其更加灵活,以便它也将接受与您相同的参数作为输入可以传递到url_for. 问题是,url_for返回一个完整的 URL,包括协议/主机,而我只想要路径部分...
url_for有一个可爱的only_path: true选项,可以跳过添加协议/主机。只要您将其作为单个哈希的一部分传递给url_for:
main > app.url_for(controller: \'users\', action: \'index\', only_path: true)\n=> "/users"\nRun Code Online (Sandbox Code Playgroud)\n\n但是,当您将数组或模型对象传递给url_for时,如何传递选项呢?
main > app.url_for(user, only_path: true)\nArgumentError: wrong number of arguments (given 2, expected 0..1)\nfrom /gems/actionpack-5.1.6/lib/action_dispatch/routing/url_for.rb:166:in `url_for\'\n\nmain > app.url_for([user, :notification_preferences], only_path: true)\nArgumentError: wrong number of arguments (given 2, expected 0..1)\n/actionpack-5.1.6/lib/action_dispatch/routing/url_for.rb:166:in `url_for\'\nRun Code Online (Sandbox Code Playgroud)\n\n你不能!显然,如果您传递散列以外的任何内容,则在语法上甚至不可能传递选项散列,因为它的数量是0..1并且只需要一个参数:url_for(options = nil)。
我在 react redux 中使用 typescript 有一个 react 应用程序。我正在使用 jest 编写测试。根据惯例,我对测试文件的扩展名应该是什么感到困惑。无论是 js/ts 还是 tsx。
任何线索将不胜感激。
作为我之前问题的后续......
valueProp有没有办法在以下示例中提供默认值?
type ValueType = 'value' | 'defaultValue'
type Props<T extends ValueType> =
Record<T, string>
& { other: string }
function props<T extends ValueType>(valueProp: T): Props<T> {
return {
[valueProp]: 'value',
other: 'other',
} as Props<T>
}
let { defaultValue, other } = props('defaultValue') // ok
let { value } = props('value') // ok
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时:
function props<T extends ValueType>(valueProp: T = 'value'): Props<T> {
return {
[valueProp]: 'value',
other: 'other',
} as Props<T>
}
Run Code Online (Sandbox Code Playgroud)
,我收到此错误:
Type '"value"' …Run Code Online (Sandbox Code Playgroud) 谁能告诉我为什么这个应用程序会遇到“称为外部组件初始化的函数”错误?(更新:找到了这个特定错误的原因,但仍然有以下关于将汇总与 svelte 库一起使用的最佳实践的问题。)
它似乎只在我从循环内的组件(应该允许)调用getContext(或onMount等)时发生。但只有当我包含在库中时才会发生,所以这可能是一个汇总问题而不是一个 Svelte 问题。{#each}external: ['svelte']
这是我的代码(您可以从这里克隆并自己尝试):
"dependencies": {
"my-new-component": "file:packages/my-new-component",
…
}
Run Code Online (Sandbox Code Playgroud)
src/App.svelte:
<script>
import { FieldArray } from "my-new-component";
import { UsesContext } from "my-new-component";
</script>
<FieldArray let:names>
{#each names as name, i}
<div>{name}: <UsesContext /></div>
{/each}
</FieldArray>
Run Code Online (Sandbox Code Playgroud)
packages/my-new-component/src/FieldArray.svelte:
<script>
let names = ['a']
const handleClick = () => {
names = ['a', 'b']
}
</script>
<button on:click={handleClick}>Blow up</button>
<slot names={names} /> …Run Code Online (Sandbox Code Playgroud) 这看起来很基本,但我使用的是 Rails 5,我将所有视图都换成了 HAML 而不是 ERB。现在我的一项测试未能说出以下内容:
ActionController::UnknownFormat: ProductsController#index is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
test/controllers/products_controller_test.rb:9:in `block in <class:ProductsControllerTest>'
Run Code Online (Sandbox Code Playgroud)
有什么地方可以更改默认值,还是我必须解决其他问题?我的路线很好,我可以访问索引页面没问题。
ruby ×4
angularjs ×2
typescript ×2
erb ×1
go-templates ×1
haml ×1
jestjs ×1
reactjs ×1
redux ×1
regex ×1
rollupjs ×1
svelte ×1
syntax ×1
syntax-error ×1