如何存储比通过Angular中的Output()和EventEmitter()传入的最近值更多的值?(How do I store more than the most recent value passed in via Output() and EventEmitter() in Angular?)
我在我的Angular应用程序中使用自定义输出来在组件之间发送用户选择的值。 现在工作的是,每当用户点击一个复选框项时,该值就会发送到我的子组件,在那里我可以看到它打印到控制台。 问题是,当用户点击另一个选择,我得到该值发送,但以前发送的值现在返回未定义。 换句话说,我只能获得最近的价值。 我的问题是:如何存储传递的值,以便我可以用每个用户选择的值构建一个过滤器列表,而不是最新的值?
这些值通过Angular的Output()和EventEmitter()传递给子组件,并发送给一个“getFilters($ event)”函数,如下所示:
<div class="page-content"> <list [records]="records" (sendChange)="getFilters($event)" (sendLanguage)="getFilters($event)" (sendBranch)="getFilters($event)" (sendZipcode)="getFilters($event)"> </list> </div>
我试图接收这些值,然后将它们存储在变量中。 但是当我控制台记录这些变量时,我仍然只看到最近的选择。 我如何存储所有选择以构建新的查询?
getFilters(page, language, zipcode, branch) { const paged = page; const lang = language; const zip = zipcode; const city = branch; console.log('Stored: ' + paged + ' ' + lang + ' ' + zip + ' ' + city); }
所以,如果“zipcode”是传入的最后一个值,这就是我在控制台中看到的 - (所有其他先前发送的值现在都是“undefined”):
Stored: 90001 undefined undefined undefined undefined
I am using custom outputs in my Angular app to send user-selected values between components. Right now what's working is that each time a user clicks a check-box item, that value is sent through to my sub-component, where I can see it print to the console. The problem is that when the user clicks another selection, I get that value sent through, but the previous sent value now returns undefined. In other words, I'm only ever getting the most recent value. My question is: how do I store those values being passed down, so that I can built a filter list with each of the user-selected values, rather than just the most recent one?
The values are being passed down to the sub-component with Angular's Output() and EventEmitter(), and sent to a "getFilters($event)" function, like this:
<div class="page-content"> <list [records]="records" (sendChange)="getFilters($event)" (sendLanguage)="getFilters($event)" (sendBranch)="getFilters($event)" (sendZipcode)="getFilters($event)"> </list> </div>
I have tried receiving those values, and then storing them in variables. But when I console log those variables, I'm still only ever seeing the most recent selection. How can I store all of the selections to be able to build a new query?
getFilters(page, language, zipcode, branch) { const paged = page; const lang = language; const zip = zipcode; const city = branch; console.log('Stored: ' + paged + ' ' + lang + ' ' + zip + ' ' + city); }
So, if "zipcode" was the last value passed in, this is what I see in the console - (all other previously sent values are now "undefined"):
Stored: 90001 undefined undefined undefined undefined
原文:https://stackoverflow.com/questions/43672454
最满意答案
如果你不想在数据库中创建任何东西,你可以这样做:
employee = mock_model(Employee) task = mock_model(Task, name: "Do that", employee: employee)
请记住,你不能像那样查询他们。 它与构建对象大致相同。 如果你想做任何你需要查询实际数据的东西,比如集成测试,那么你需要使用
create
来create
数据库中的东西。 或者正如一位评论者指出的那样,您可以使用FactoryGirl的方法来删除东西。If you don't want to create anything in the database you can do this:
employee = mock_model(Employee) task = mock_model(Task, name: "Do that", employee: employee)
Keep in mind that you can't query them like that. It's roughly the same as building the object. If you ever want to do anything where you need to query actual data such as an integration test then you'll need use
create
to make stuff in the database. Or as one commenter pointed out, you can use FactoryGirl's methods to stub stuff out.
相关问答
更多-
你的模特应该是这样的: class Product < ActiveRecord::Base has_many :orders end class Order < ActiveRecord::Base belongs_to :product belongs_to :user end 现在你可以这样做: Order.joins(:product).all 但是你想要实现什么目标? 这个毫无意义的加入是什么原因? 如果您只想预加载产品,以便对数据库没有任何其他查询,则可以使用includes而 ...
-
应该做的伎俩 named_scope :without_users, { :include => :users, :conditions => 'image_shell_users.user_id IS NULL' } 使用include选项时,ActiveRecord会为您生成所有正确的连接。 为了包含用户必须通过连接表,因此您可以在您的条件中使用它。 Should do the trick named_scope :without_users, { :include => :users, ...
-
如何保存与ActiveRecord Ruby On Rails的关联(How to save associations with ActiveRecord Ruby On Rails)[2023-06-12]
调用tasks.create(task_params)你实际上是为类型和用户创建一个新任务。 相反,您只想设置如下关联: def create @task = Task.new(task_params) @user = User.find(5) @type = Type.find(params[:type_id]) @task.user = @user # Just set the reference! @task.type = @type # Just set the referenc ... -
最短的拼写可能是: Event.where.not(id: TimeSlot.current_or_future.select(:event_id)) 在TimeSlot上定义范围: scope :current_or_future, -> { where(to: DateTime.current..DateTime::Infinity) } The shortest spelling is probably: Event.where.not(id: TimeSlot.current_or_future ...
-
该选项(当然,在Rdoc中有详细记录)是:class_name。 我相信语法是: has_one :creditor, :class_name => 'User' 此外,您可能想要为此添加书签: http://api.rubyonrails.org/ 下次可以节省几分钟。 The option (naturally, well-documented in the Rdoc) is :class_name. I believe the syntax is: has_one :creditor, :class ...
-
你需要多对多的关系。 在轨道有2种方式来做到这一点。 你可以在这里阅读。 一般来说,您需要将has_and_belongs_to_many添加到Sponsor并Match 。 并创建包含match_id + sponsor_id 'join-model'。 通过这种方式, ActiveRecord将能够通过'join-table'创建合适的SQL查询。 You need many-to-many relationship. In rails where are 2 ways to do this. You ...
-
试试看 class Author < ActiveRecord::Base has_many :books, :validate => false validates_presence_of :email after_save :save_invalid_books def save_invalid_books books.each do |b| b.save(false) end end end 据我所知,validate => false只允许您保存作 ...
-
我会建议用SQL来做。 你可以通过几种方式来完成。 你使用MYSQL吗? 如果你想处理2个查询,然后将这些数字加在一起,你可以在SQL中做这样的事情: select count(starrings.id) from starrings where starrable_type='BlogPost' and starrable_id=#{blogpost.id} 和 select count(starrings.id) from starrings join replies on starrings.star ...
-
如果你不想在数据库中创建任何东西,你可以这样做: employee = mock_model(Employee) task = mock_model(Task, name: "Do that", employee: employee) 请记住,你不能像那样查询他们。 它与构建对象大致相同。 如果你想做任何你需要查询实际数据的东西,比如集成测试,那么你需要使用create来create数据库中的东西。 或者正如一位评论者指出的那样,您可以使用FactoryGirl的方法来删除东西。 If you don't ...
-
您必须将accounts表加入users表,然后检查一个空帐户。 在Rails 3中,你可以这样做: User.includes(:account).where('accounts.id' => nil).all 在Rails 2中,你可以这样做: User.find(:all, :include => [ :account ], :conditions => { 'accounts.id' => nil }) You have to join the accounts table into the u ...