AngularJS: die Fabrik ist immer undefiniert, wenn Sie injiziert in controller

Ich versuche ein einfaches Beispiel einer Adressbuch-Winkel-Anwendung. Ich habe eine Fabrik, die gibt ein array von Datensätzen und es wird angezeigt in einer Liste Ansicht mit einer Liste-controller

angular.module('abModule', ['ngRoute'])
.factory('AddressBook', function() {
    var address_book = [
        {
            "id": 1,
            "first_name": "John",
            "last_name": "Doe",
            "age": 29
        },
        {
            "id": 2,
            "first_name": "Anna",
            "last_name": " Smith",
            "age": 24
        },
        {
            "id": 3,
            "first_name": "Peter",
            "last_name": " Jones",
            "age": 39
        }
    ];
    alert('inside factory function');
    return {
        get: function() {
            return address_book
        }
    };
})
.config(function($routeProvider) {
    $routeProvider
    .when('/', {
        controller:'list_controller',
        templateUrl:'list.html'
    })
    .when('/add', {
        controller:'add_controller',
        templateUrl:'add.html'
    })
    .otherwise({
        redirectTo:'/'
    });          
})
.controller('list_controller',['$scope', function ($scope, AddressBook) {
    $scope.address_book = AddressBook;
}])
.controller('add_controller',['$scope', function ($scope,AddressBook) {
    //$scope.entry = {};
    $scope.save = function() {
        AddressBook.set(
            {
                "id": $scope.address_book.length +1,
                "first_name":$scope.entry.firt_name,
                "last_name":$scope.entry.last_name,
                "age":$scope.entry.age
            }
        );
    };
}]);

Hier 'AddressBook' ist immer undefined inside 'list_controller'. Keine Ahnung, wo ich bin mache ich falsch? Jede Hilfe wird sehr geschätzt.

  • Ihr Adressbuch dont haben set-Funktion
Schreibe einen Kommentar