Complete the code to register a simple custom directive named 'focus' that sets focus on the element.
app.directive('focus', { mounted(el) { el.[1]() } })
The focus method sets the keyboard focus on the element.
Complete the code to define a custom directive that changes the element's background color to the value passed.
app.directive('bg-color', { mounted(el, binding) { el.style.backgroundColor = [1] } })
binding.arg which is for argument names, not values.binding.modifiers which is an object of flags.binding.instance which refers to the component instance.The binding.value holds the value passed to the directive, used here to set the background color.
Fix the error in the directive that tries to log the element's tag name when mounted.
app.directive('log-tag', { mounted(el) { console.log(el.[1]) } })
The correct property to get the tag name is tagName with capital N.
Fill both blanks to create a directive that updates the element's text content when the bound value changes.
app.directive('text-update', { mounted(el, binding) { el.textContent = [1] }, updated(el, binding) { el.textContent = [2] } })
binding.oldValue which is the previous value, not the current.binding.arg or binding.modifiers which are unrelated.Both mounted and updated hooks should set el.textContent to binding.value to reflect the current value.
Fill all three blanks to create a directive that adds an event listener on mount, removes it on unmount, and uses the handler passed as the value.
app.directive('on-click', { mounted(el, binding) { el.addEventListener('click', [1]) }, unmounted(el, binding) { el.removeEventListener('click', [2]) }, updated(el, binding) { el.removeEventListener('click', [3]) el.addEventListener('click', binding.value) } })
binding.arg which is not a function.The directive uses binding.value as the current handler and binding.oldValue to remove the previous handler when updated or unmounted.