Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent html attribute of KnockoutJs's WITH in AngularJs

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.

like image 695
CraigM Avatar asked Feb 03 '26 04:02

CraigM


1 Answers

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.

like image 200
Krishna Veeramachaneni Avatar answered Feb 04 '26 18:02

Krishna Veeramachaneni