2

I'm trying to learn Angular, and I'm stuck on the following. I have a PHP background, mostly use Laravel, and in Laravel you can use accessors in your models. So if you have a model User, which has a firstname and lastname. You can create an accessor for fullname which will return both the firstname and lastname:

public function getFullNameAttribute()
{
    return $this->firstname . ' ' . $this->lastname;
}

Now, I would like to do the same thing in Angular. I have a UserController with a user:

function UserController($scope) {
    $scope.users = [
        { firstname: 'John', lastname: 'Doe' }
    ];
}

In the "view" I want to loop over the users with ng-repeat, but instead of doing

{{ user.firstname }} {{ user.lastname }}

I would like to be able to just use

{{ user.fullname }}

Maybe it's called differently in Angular or something, but I just can't seem to find it...

Thanks for the help!

3 Answers 3

3

Angular does not yet natively provide model abstractions (beside simplistic $resource service which deals mostly with RESTfull communication layer). But that doesn't mean you can't write and re-use your own custom model accessors.

Option 1 - Use custom filter

PLUNKER

app.filter("fullName", function () {
  return function (user){
    return user && [user.firstName, user.lastName].join(' ');
  }
});

app.controller("MainCtrl", [
  "$scope", 
  function($scope) {
    $scope.user = {
      firstName: 'John', 
      lastName: 'Doe'
    };
    
  }
]);
User fullName: {{user | fullName}}

 

Option 2 - Use custom $resource instance accessors:

PLUNKER

app.factory('UserService', function ($resource){
  // User is an instance of $resource service which 
  // in this example uses mock server endpoint
  var User = $resource('user.json');
  
  // Custom User accessor method
  User.prototype.fullName = function(){
    return [this.firstName, this.lastName].join(' ');
  }
  
  return User;
});


app.controller("MainCtrl", [
  "$scope", 
  "UserService",
  function($scope, UserService) {
    $scope.user = UserService.get()
    
  }
]);
User fullName: {{user.fullName()}}
Sign up to request clarification or add additional context in comments.

1 Comment

option 2 is pretty slick
2

In addition to Stewie's very sophisticated techniques, you can also use ng-init.

<div ng-repeat="user in users" 
     ng-init="user.fullname = user.firstname + ' ' + user.lastname">
  {{user.fullname}}
</div>

http://plnkr.co/zU6vM5f8pI3Veh7jr1R9

Comments

0

angular does not have a model framework, here your scope is the model and any javascript object (plain old javascript object) attached to it. You might be interested in this article which talks about implementing an OO model layer on angular js. https://medium.com/opinionated-angularjs/2e6a067c73bc. Like others have said, implementing filters or directives might be the easiest and is also reusable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.