Controller as in Angular 1.2

Demonstrates usage of controller as in a view, in a route and in a directive

by guilbep

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="failCtrl">
        <select ng-options="a for a in types track by a" class="form-control" ng-model="obj.selectedType"></select>
    </div>
</div>

JavaScript

var app = angular.module("myApp", []);
// angular 1.2.25
app.controller("failCtrl", function ($scope, $timeout) {
    // last element not selected
    $scope.types = ["a", "b", "c"];
    // will work with one more element
    // $scope.types = ["a", "b", "c", "d"];
    var init = function () {
        $scope.obj = {
        };
    };
    $timeout(init, 1000);
    $scope.$watch('obj', function (elem) {
        if ( !!elem) {
            elem.selectedType = elem.selectedType || $scope.types[2];
        }
    }, true);
});