ラベル AngularJS の投稿を表示しています。 すべての投稿を表示
ラベル AngularJS の投稿を表示しています。 すべての投稿を表示

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

    2013年6月30日日曜日

    Setting up angularjs on Windows 7

    My ultimate goal was to try out AngularJS, which supposedly gives us MVC in JavaScript world.

    For our game, which is written in .NET stack, I recently set up REST API and WebSocket, and about to embrace JavaScript-oriented browser client.

    Also, my colleague at work set up a web app (there, we use Java as the server) using YEOMAN.

    The world of JavaScript development is pretty alien to me. I've used JQuery and I know basic HTML5/CSS3 stuff, but not much more than that.

    This is just a log of what actions I've taken to get to the point where I have a AngularJS app running (probably integrated to ASP.NET MVC3 - or maybe side-by-side).

     Its work in progress so I may update this later.


    1. Installed Chocolately that gives us a mechanism to install all kinds of software, many of which we know and familiar with (but we use separate "Installers" to install it on Windows). Chocolately let you install at command line.
    2. I used Chocolately to install Yeoman as:
      1. cinst yeoman
    3. To scaffold a web app, this is the Yeoman command line I used, which worked
      1. yeoman init
    4. I was curious about other Yeoman command. One of them is "Yeoman list" to list all packages, but I run into an error:
      1. yeoman list
      2. Error Arguments to path.join must be strings
      3. I found an article that suggests a fix, but I could just not find the file to put a patch (even when I grepped the entire drive. I must be doing something stupid)
    5. I gave up and I decided to do clean install again. By this time, I realized that nodeJS's Node Plugin Manager (nmp) is the tool to install the tools that comprises YEOMAN, namely, yo, grunt, and bower. So I installed them one by one (I basically just followed the "installation" instruction at yeoman.io website.)
      1. npm install -g yo grunt-cli bower
      2. npm install -g generator-webapp
    6. At this point, I believe I have all the tools I needed, some were installed as part of Chocolately install, some other manually by hand. Verify this by just obtaining version number of each tool
      1. node --version
      2. git --version
      3. yo --version
      4. grunt --version
      5. bower --version
      6. ruby --version
      7. compass --version
    7. Once I got these tools installed, then I proceeded to generate AngularJS project (again, just follow "Usage" instruction at yeoman.io website.)
      1. npm install -g generator-angular
      2. yo angular
    This is as far as I got. AngularJS project has been created but I see bunch of errors too and not sure if they are show-stopper or not.