Coder Social home page Coder Social logo

angular-scope-with's Introduction

Angular scope-with

Adds ability to scope an object with a certain property in a template.

When using objects with nested properties, it can become tedious to display child properties in an angular template.

For Example

  PersonController($scope) {
    $scope.person = {
      name: 'Alice Jane',
      nickname: 'Ali',
      age: '40',
      
      adddress: {
        street: '77 Mump Bump Road',
        country: 'USA',
        state: 'CI'
      }
    
    };
   
  }

Template

  <div ng-controller="PersonController">
    <div class="person">
        Name: {{person.name}} <br />
        Nickanme: {{person.nickname}} <br />
        Age: {{person.age}} <br />
        Address: <br />
        {{person.address.street}}<br />
        {{person.address.country}}<br />
        {{person.address.state}}
    </div>
  </div>

Typing out all the properties can become tedious and spoil your template code.

Scope With

The scope with directive is designed to scope the properties and transcludes them within the defined block:

Template using scope-with

Re-using the same object we can modify our code to read as follows.

<div ng-controller="PersonController">
   <div class="person" scope-with="person">
      Name: {{name}} <br />
      Nickanme: {{nickname}} <br />
      Age: {{age}} <br />
      Address: <br />
      {{address.street}}<br />
      {{address.country}}<br />
      {{address.state}}
   </div>
</div>

Or:

<div ng-controller="PersonController">
   <div class="person" scope-with="person">
      Name: {{name}} <br />
      Nickanme: {{nickname}} <br />
      Age: {{age}} <br />
      Address: <br />
      <address scope-with="address">
        {{street}}<br />
        {{country}}<br />
        {{state}}
      </address>
   </div>
</div>

Using the directive

In your module, simply register the scope-with module as a dependency, or just copy and paste the directive code.

angular.module('myapp', ['al.directive.scopewith']).controller('PersonController', PersonController);

Working Demo

http://embed.plnkr.co/OJDhpJ1maEdSoPvlbiRA/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.