首页 \ 问答 \ 如何存储比通过Angular中的Output()和EventEmitter()传入的最近值更多的值?(How do I store more than the most recent value passed in via Output() and EventEmitter() in Angular?)

如何存储比通过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
更新时间:2024-02-11 22:02

最满意答案

如果你不想在数据库中创建任何东西,你可以这样做:

employee = mock_model(Employee)
task = mock_model(Task, name: "Do that", employee: employee)

请记住,你不能像那样查询他们。 它与构建对象大致相同。 如果你想做任何你需要查询实际数据的东西,比如集成测试,那么你需要使用createcreate数据库中的东西。 或者正如一位评论者指出的那样,您可以使用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, ...
  • 调用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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)