Wednesday, February 10, 2016

Prototypal inheritance - page 53 AngualrJS for .Net developers in 24 hours,

Prototypal inheritance is a concept that gives a lot of JavaScript developers problems, especially coming from a classical inheritance background. It’s a vitally important concept, though. To read more about prototypal inheritance, check out the Mozilla Developer Network’s JavaScript Guide. It’s full of great information on JavaScript.

<!-- View without ControllerAs syntax that has nested scopes.-->
<div ng-controller="Order">
    <input type="text" ng-model="orderTotal" />
    <div ng-if="showDetails">
        <input type="text" ng-model="orderTotal" />
    </div>
</div>

You can avoid all of that mess by using ControllerAs syntax:
<!-- View with nested scopes, but with ControllerAs.-->
<div ng-controller="Order as vm">
    <input type="text" ng-model="vm.orderTotal" />
    <div ng-if="showDetails">
        <input type="text" ng-model="vm.orderTotal" />
    </div>
</div>

Now the two models explicitly share the same scope of the controller’s alias, vm, and we’ve tamed the scope craziness.

No comments:

Post a Comment