0

I have an Angular application that get a 'person' from a rest call. In the config I have:

        when('/people/edit/:id',{
        controller:PersonEditCtrl,
        templateUrl: 'frontend/partials/people/person.html',
        resolve: {
            person: function(Restangular, $route){
                var theRoute= 'people/' + $route.current.params.id + '/';
                return Restangular.one(theRoute).get();
            }
        }
    }).

In the controller:

function PersonEditCtrl($scope, $location, Restangular, person) {

      $scope.person = person;
 }

In the html page, I use like this to show info:

{{person.firstName}}

In the html page, I would like to add some behavior to 'person'. For instance, I'd like to add a function that combines the first and last name. So, I'd like a function like getFullName(). Note that I am not making a person object, I am only getting the JSON from a ReST call. I'm assuming the a person object is getting made somehow, but I do not know how.

How do I add functions/methods to 'person' in how I currently have things?

7
  • You get the object from transforming the json response into javascript, since this is a normal javascript object, you can add methods to that object just like to any other object, through dot or square brackets notation. Commented Mar 29, 2015 at 16:40
  • In my controller, this is not working: $scope.person.foo= function(){return this.firstName + " " + this.lastName; }...should it? Commented Mar 29, 2015 at 16:43
  • Please explain what do you mean by "not working"? Isn't the method added to the object, isn't the method visible in the view or does the method return incorrect data? Commented Mar 29, 2015 at 16:47
  • Not working i mean in the page, this does not print anything: {{person.foo}} Commented Mar 29, 2015 at 16:51
  • Because you don't call it. It's a function, so call it! ;D Commented Mar 29, 2015 at 16:52

1 Answer 1

2

Set person.getFullName = function(){ //Code here }

Sign up to request clarification or add additional context in comments.

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.