tempid
tempid

Reputation: 8208

Separate models from controllers

I have something like this -

function DetailCtrl($scope) {
    $scope.persons = [{
        id: 1,
        name: "Mark"
    }];        
}

I'd like to keep the models separate from the controller, like this -

    //models
    var person = { id: '', name: '' };

    function DetailCtrl($scope) {
       person = db.getPerson();
       $scope.person = person;
    }

Is this a good practice in angularjs? I come from ASP.NET MVC background.

Upvotes: 4

Views: 2138

Answers (1)

Mark Rajcok
Mark Rajcok

Reputation: 364697

Yes, it is a best practice to have your models elsewhere, and have your scopes reference your models: listen to 2 minutes of Misko from his "Best Practices" video.

Services are a good place to store your models.
Brandon has a good answer related to this: https://stackoverflow.com/a/14667066/215945

Upvotes: 3

Related Questions