我对我应该使用什么项目类型感到困惑。在官方 Ionic 文档中,他们建议通过运行 - 来创建一个新项目ionic start MyApp tutorial --type=ionic-angular。但是从其他来源我发现我可以ionic start MyApp tutorial --type=angular,这让我的生活更轻松,因为我可以使用 for ex。ng add添加依赖项,项目结构类似于 Angular。但是,--type=angular没有super项目类型,所以我不能用 生成新的应用程序ionic start MyApp super --type=angular,我可以忍受,但是需要一些关于首选哪种项目类型的建议 - 选择一个(--type=angularvs --type=ionic-angular)的标准是什么?
我的问题已经解决了,但我认为这有点笨拙,所以希望能找到更好的方法,以及更好地理解这里发生的事情。
_form.html.erb:
<%= select_tag(:category_id, options_for_select(@categories), :prompt => "Select one!") %>
Run Code Online (Sandbox Code Playgroud)
产品控制器.rb:
def new
@product = Product.new
@categories = Category.all.map { |c| [ c.name, c.id ] }
end
Run Code Online (Sandbox Code Playgroud)
如果用户提交表单而没有选择任何选项,select_tag他会收到undefined method 'map' for nil:NilClass错误。
我知道它是因为我的@categories存在而出现的nil,但我不知道如何避免这种情况..?
我的最终解决方案有效:
<%= select_tag(:category_id, options_for_select(@categories || Category.all.map { |c| [ c.name, c.id ] }), :prompt => "Select one!") %>
Run Code Online (Sandbox Code Playgroud)
但我觉得还有更好的方法。我还认为,通过分配默认select_tag值:selected也可能有效,但我无法用我的 Ruby 语法知识来实现它......
使用此功能,我生成所需的范围:
first_index = 0
last_index = 3
ranges = []
while first_index != last_index
while last_index != 0
if first_index < last_index
ranges << (first_index..last_index)
end
last_index -= 1
end
first_index += 1
last_index = 3
end
p ranges
Run Code Online (Sandbox Code Playgroud)
输出是:
[0..3, 0..2, 0..1, 1..3, 1..2, 2..3]
Run Code Online (Sandbox Code Playgroud)
我需要while在完成后恢复嵌套循环的输出.所以在这个例子中,我需要:
[0..3, 0..2, 0..1].reverse
[1..3, 1..2].reverse
[2..3].reverse (wouldn't make any different on this, though)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
[0..1, 0..2, 0..3, 1..2, 1..3, 2..3]
Run Code Online (Sandbox Code Playgroud)
我能reverse以某种方式调用该函数吗?last_index可以是任何整数.我用3来缩短输出.
无法处理数组过滤。这个:
@profiles = Profile.filter(params.slice(:location, :status, :items))
Run Code Online (Sandbox Code Playgroud)
传递要过滤的记录:
def filter(filtering_params)
results = self.where(nil)
puts filtering_params
filtering_params.each do |key, value|
if value.is_a?(Array)
results = results.public_send(key, value.to_s) if self.where("#{key}", value)
else
results = results.public_send(key, value) if value.present?
end
end
results
end
Run Code Online (Sandbox Code Playgroud)
但是 Postgres 数组存在一些问题。传递给filter方法的参数是:
参数:{"utf8"=>"?", "location"=>"0", "status"=>"0", "items"=>["0", "1"], "commit"=> "过滤器"}
和方法接收它们:
{“位置”=>“0”,“状态”=>“0”,“物品”=>[“0”,“1”]}
但我得到错误:
PG::InvalidTextRepresentation: 错误: 格式错误的数组文字: "0" LINE 1: ..." = $2 AND "profiles"."items" IN ('0', '1') ... ^
详细信息:数组值必须以“{”或维度信息开头。: SELECT "profiles".* FROM "profiles" WHERE "profiles"."location" = $1 AND "profiles"."status" …
Angular Material 表利用 MatTableDataSource 方法来sortData()对表的数据源进行排序。(https://material.angular.io/components/table/api#MatTableDataSource)。它是一个箭头函数:
sortData: ((data: T[], sort: MatSort) => T[])
Run Code Online (Sandbox Code Playgroud)
我需要查看排序后的数据而不实际更改排序。例如,
this.dataSource.sortData = (sortData) => {
console.log(sortData);
return sortData;
};
Run Code Online (Sandbox Code Playgroud)
这里的问题是我重写了本机排序功能。return sortData返回原始数据,不进行任何排序。我想做的就是观察排序后的数据而不修改它。是否有可能做到这一点?
angular-material angular angular-material-table angular-material-7
我在 codecadamy.com 上的 Javascript 教程午餐时间。
我有这个简单的循环:
var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';
while (currentCard !== 'Spade') {
console.log(currentCard);
var randomNumber = Math.floor(Math.random() * 4);
currentCard = cards[randomNumber];
}
console.log('Found a Spade!');Run Code Online (Sandbox Code Playgroud)
这很好,但是如果我randomNumber从循环中删除并与其他全局变量一起放置,codecademy 控制台不会打印结果......
由于全局变量应该可用于该循环,我认为 codecademy 控制台有问题?
你同意这应该有效吗?
var cards = ['Diamond', 'Spade', 'Heart', 'Club'];
var currentCard = 'Heart';
var randomNumber = Math.floor(Math.random() * 4);
while (currentCard !== 'Spade') {
console.log(currentCard);
currentCard = cards[randomNumber];
}
console.log('Found a Spade!');Run Code Online (Sandbox Code Playgroud)
这是一个简单的ruby脚本,它接受来自用户的输入并提供输出(是的,它将被重构).我希望这个脚本能够将输出提供给文本文件,而不是控制台窗口.这是通过简单添加完成的,$stdout = File.new('out.txt', 'w')但我认为这一行只会描述一个变量,我将在稍后使用它告诉脚本使用它将输出写入创建的文件.
我找不到有关此方法的大量文档,并想知道该程序如何知道如何将生成的输出写入该文件?
我有几个小时的数字,我需要将它转换为几小时和几分钟.
例如,我有这个小时数 33.22
我需要将它转换为几小时和几分钟.
结果应该是01.09:13:12(即一天9小时,13分钟和12秒).
目前我这样做:
value = 33.22
var x = new Date(value * 3600000); //replace with TotalMilliSeconds in your case
var time = x.toUTCString().split(' ')[4]; //slit by space character
return time.split(':')[0] + ":" + time.split(':')[1];
Run Code Online (Sandbox Code Playgroud)
但结果是我错了我认为我应该避免使用Date对象.
如果我有小时数,怎么能得到几小时和几分钟?
我有两个哈希:
first = { 1 => [15, 15, 15, 8], 4 => [11, 12, 7, 7], 5 => [14, 17, 13, 13],
6 => [19, 19, 15, 15], 7 => [5, 12, 12, 12], 8 => [10, 14, 14, 14],
9 => [8, 7, 8, 8] }
second = { 1 => [0, 1, 2], 4 => [2, 3], 5 => [2, 3], 6 => [0, 1, 2, 3],
7 => [1, 2, 3], 8 => [1, 2, 3], 9 …Run Code Online (Sandbox Code Playgroud) 我创建了一个非常丑陋的脚本来从数组中收集相同的数字.我不认为这是一种非常Ruby的方式:)任何人都可以提供更干净的解决方案吗?
ar = [5, 5, 2, 2, 2, 6, 6]
collections = []
collect_same = []
while ar.length > 0
first = ar.values_at(0).join.to_i
second = ar.values_at(1).join.to_i
if ar.length == 1
collect_same << ar[0]
collections << collect_same
break
else
sum = ar.values_at(0, 1).inject {|a,b| a + b}
if second == first
p collect_same << ar[0]
ar.shift
else
collect_same << ar[0]
collections << collect_same
collect_same = []
ar.shift
end
end
end
p collections
Run Code Online (Sandbox Code Playgroud)
输出:
=> [[5, 5], [2, 2, 2], [6, 6]] …Run Code Online (Sandbox Code Playgroud) ruby ×5
angular ×2
arrays ×2
javascript ×2
while-loop ×2
android ×1
form-for ×1
ionic4 ×1
jquery ×1
postgresql ×1
refactoring ×1
reverse ×1
ruby-hash ×1
stdout ×1
sum ×1