Angular Models

by johnlindquist

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 Authors.list" ng-click="Authors.selectedAuthor = author"
                   class="{{Authors.selectedAuthor == author && 'selected' || ''}}">{{author.name}}</a>
            </div>
            <div class="large-10 columns end padtop">
                <textarea name="quotetext" id="quotetext" readonly>{{Authors.selectedAuthor.quote}}</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', 'Authors', function ($scope, Authors) {
	$scope.Authors = Authors;
}]);

angular.module('modelDemo').service("Authors", [function () {
	var fowler = {
			name: "Fowler",
			quote: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
		},
		twain = {
			name: "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."
		},
		poe = {
			name: "Poe",
			quote: "Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before."
		},
		plato = {
			name: "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. "
		};

	this.list = [fowler, twain, poe, plato];

	this.selectedAuthor = this.list[0];
}]);