Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
Knockout JS - Update view visibility when observable changes value

So I was trying to update an span tag visibility when a value changes, but noticed that using if: and directly using the compare operator there doesn't work when the value changes. For example:

<span data-bind="if: firstName === 'Bert'">Hello</span>

So if we have initially something else in the firstName observable it will not be visible. Theorically if we change the value to Bert (maybe using a text input) or a function that changes the value it should show the Hello span, but it isn't working.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To update the HTML component using the if statement, we should use a function in the JS and call the function like below.

In the JS (inside the view model):

this.showSpan = function() {
    return this.firstName() === 'Bert'
}

And in the HTML:

<span data-bind="if: showSpan()">Hello</span>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda