2013年7月18日木曜日

How to render JSON key-value properties using AngularJS

See a demo at JSFIDDLE


When you are developing REST API on server side, you sometimes want to have a page that renders JSON data in a bit nice way than a row JSON view (that most browsers do good job rendering, but still somewhat hard to read)

To render JSON data in a simple HTML table (or whatever) format with minimum AngularJS coding, here is how.


JSON Data

Let's say, you have a JSON data (this is from AngularJS Tutorial) available from a controller ("C" part of MVC)

function PhoneListCtrl($scope) {
    $scope.phones = [
        {
            "name": "Nexus S",
            "snippet": "Fast just got faster with Nexus S.",
            "age": 0},
        {
            "name": "Motorola XOOM with Wi-Fi",
            "snippet": "The Next, Next Generation tablet.",
            "age": 1},
        {
            "name": "MOTOROLA XOOM",
            "snippet": "The Next, Next Generation tablet.",
            "age": 2}
    ];
}

HTML markup 

...with AngularJS expressions. Note the expression "(key, value) in phone" below. (By the way, this is "V" part of MVC, and "phones" data is the "M" part, just for the sake of completeness!)

<div>
  <h1>Phones</h1
  <div ng-repeat="phone in phones">
    <h2>{{phone.name}}</h2>
    <table border="1px">
      <tr ng-repeat="(key,value) in phone">
        <td>{{key}}</td>
        <td>{{value}}</td>
      </tr>
    </table>
  </div>
</div>

Result

Phones

Nexus S

age0
nameNexus S
snippetFast just got faster with Nexus S.

Motorola XOOM with Wi-Fi

age1
nameMotorola XOOM with Wi-Fi
snippetThe Next, Next Generation tablet.

MOTOROLA XOOM

age2
nameMOTOROLA XOOM
snippetThe Next, Next Generation tablet.

2013年7月1日月曜日

How to run angular tutorial with IIS 7 or node.js


If you are a developer with Windows machine and want to play with AngularJS, here are the steps.

Downloading Angular tutorial

  1. Create a clone on your machine
    1. Go to https://github.com/angular/angular-phonecat
    2. Choose "Clone in Desktop" (This asks you to give permission to start GitHub windows client)
  2. Download a given "step" within the turoial
    1. Run "git checkout -f -step-0" (here 0 should change depending on which step you want)


    Running Angular tutorial using IIS

    1. Create a new test website
      1. Navigate to "Sites" folder
      2. Right-mouse click and choose "Add Web Site..."
        1. Name it "angulartest"
        2. Point to the directory where Angular tutorial web site exists
        3. Set Port value to 85 (or 86 or 8080, or whatever unused on your system)
    2. Configure permissions
      1. Right-mouse click on "angulartest" website and choose "Edit permissions..."
        1. Under "Sharing" tab, share the folder with "Everyone"
        2. Under "Security" tab, add "Everyone" to the list of users.
    3. Configure application pool for the test website
      1. In IIS, select "Application Pools"
      2. Select "angulartest" from the list
      3. Select "Basic settings..." from the Actions on the right hand side
      4. In Edit Application Pool
        1. Set "No Managed Code" to .NET Framework version
        2. Set "Classic" for Managed pipeline mode
      5. Select "Advanced settings..." from the Actions on the right hand side
        1. Set "NetworkService" at Identity property
    4. Run angular tutorial in a web browser
      1. Browse to localhost:85/app/index.html

    Running Angular tutorial using Node.JS

    1. Install node.js (just download an installer)
    2. Install http-server package
      1. Run "npm install http-server- g"
    3. Start web server
      1. CD into the root directory of Angular Tutorial
      2. Run "http-server -p85  (-p is to specify port)
    4. Run angular tutorial in a web browser
      1. Browse to localhost:85/app/index.html