Spaces:
Running
Running
File size: 1,542 Bytes
7da13d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
$data
=====
`$data` is a magic property that gives you access to the current Alpine data scope (generally provided by `x-data`).
Most of the time, you can just access Alpine data within expressions directly. for example `x-data="{ message: 'Hello Caleb!' }"` will allow you to do things like `x-text="message"`.
However, sometimes it is helpful to have an actual object that encapsulates all scope that you can pass around to other functions:
<div x-data="{ greeting: 'Hello' }"> <div x-data="{ name: 'Caleb' }"> <button @click="sayHello($data)">Say Hello</button> </div></div>Β <script> function sayHello({ greeting, name }) { alert(greeting + ' ' + name + '!') }</script>
<div x-data="{ greeting: 'Hello' }">
<div x-data="{ name: 'Caleb' }">
<button @click="sayHello($data)">Say Hello</button>
</div>
</div>
<script>
function sayHello({ greeting, name }) {
alert(greeting + ' ' + name + '!')
}
</script>
Say Hello
function sayHello({ greeting, name }) { alert(greeting + ' ' + name + '!') }
Now when the button is pressed, the browser will alert `Hello Caleb!` because it was passed a data object that contained all the Alpine scope of the expression that called it (`@click="..."`).
Most applications won't need this magic property, but it can be very helpful for deeper, more complicated Alpine utilities.
[β $root](/magics/root)
[$id β](/magics/id)
Code highlighting provided by [Torchlight](https://torchlight.dev) |