My currently using AngularJs and as I've used KnockoutJs in the past, I would like to use a similar attribute to KnockoutJs's with attribute but can't seem to find what I'm looking for. My AngularJs $scope currently looks something like this
$scope.data = {
selectedCountry: ...,
...
};
now selectedCountry is a JS object and in the HTML I would like to be able to do something like this
<div with="data.selectedCountry">
<div ng-bind="name"/>
<div ng-bind="prop1"/>
<div ng-bind="prop2"/>
<div>
instead of having to do what I'm currently doing, which is
<div>
<div ng-bind="data.selectedCountry.name"/>
<div ng-bind="data.selectedCountry.prop2"/>
<div ng-bind="data.selectedCountry.prop3"/>
<div>
is there a way to do this in AngularJs? Any help would be much appreciated. thank you.
The closest to that would be ng-init directive.
<div ng-init="c = data.selectedCountry">
<div ng-bind="c.name"></div>
<div ng-bind="c.prop1"></div>
<div ng-bind="c.prop2"></div>
</div>
Here is the plunkr
Although its not exactly same as Knockout (where you don't need an alias), it should make your life easier since alias is better than writing the full navigation property path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With