Angular Models

by lchau

HTML

<div ng-app="modelDemo">
    <div ng-view></div>

    <!-- CACHE FILE: list.html -->
    <script type="text/ng-template" id="list.html">
        <div class = "row options"> 
            <div class="large-2 columns options"> 
                 <a ng-repeat="author in list" ng-click="selectQuote(author)" class="{{isSelected(author) && 'selected' || ''}}">{{author.name}}</a>
            </div>
            <div class="large-10 columns end padtop">
                <textarea name="quotetext" id="quotetext" readonly>{{selectedQuote}}</textarea> 
             </div>        
        </div >
    </script>
    
</div>

CSS

</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/4.1.2/css/foundation.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular-resource.min.js"></script> <style> body {
    background:#e3e2e2;
}
.options {
    background:#e3e2e2;
    min-height: 190px;
}
.options a {
    display:block;
    color: #9f9f9f;
    padding:12px 10px;
    background:#e3e2e2;
    border-bottom:1px solid #cac9c9;
    position:relative;
    cursor:pointer;
}
.options a:hover {
    color: #8e8e8e;
    background:#f4f4f4;
    -webkit-transition:0.1s linear background;
    -moz-transition:0.1s linear background;
    transition:0.1s linear background;
}
.options a.selected {
    color: #6f6f6f;
    background:#fff;
}
.options a.selected:hover {
    background:#f4f4f4;
}
.options a.selected:after {
    content:"\2713";
    position:absolute;
    right:10px;
    top:50%;
    font-size:20px;
    margin-top:-10px;
}
.options a.no-border {
    border:none;
}
textarea {
    height: 175px;
    width: 100%;
    resize: none;
}
.padtop {
    padding-top: 5px;
}
}

JavaScript

angular.module('modelDemo', []).
config(function ($routeProvider) {
    $routeProvider.
    when('/', {
        controller: 'AuthorListCtrl',
        templateUrl: 'list.html'
    }).
    otherwise({
        redirectTo: '/'
    });
});

angular.module('modelDemo').controller("AuthorListCtrl", ['$scope', function($scope) {
    $scope.list;
    var fowler = {
        name: "Fowler"
    };
    var twain = {
        name: "Twain"
    };
    var poe = {
        name: "Poe"
    };
    var plato = {
        name: "Plato"
    };
    
    twain.quote = "Why, I have known clergymen, good men, kind-hearted, liberal, sincere" +
        ", and all that, who did not know the meaning of a 'flush.' It is enough " +
        "to make one ashamed of one's species.";
    fowler.quote = "Any fool can write code that a computer can understand. " +
        "Good programmers write code that humans can understand.";
    poe.quote = "Deep into that darkness peering, long I stood there, wondering, " +
        "fearing, doubting, dreaming dreams no mortal ever dared to dream before.";
    plato.quote = "All things will be produced in superior quantity and quality, and with greater ease, " +
        "when each man works at a single occupation, in accordance with his natural gifts, " +
        "and at the right moment, without meddling with anything else. ";
    
    $scope.list = [twain, fowler, poe, plato];   
    
    $scope.selectQuote = function(author) {
        $scope.selectedQuote = author.quote;
        $scope.selectedAuthor = author;
    }
    
    $scope.isSelected = function(author) {
        return author === $scope.selectedAuthor;
    }
}]);