angularjs initiating controller scope from a service

user3213420

I want to be able to preserve scope data between route changes, so I created a service to return saved data to a controller for it to initiate on scope.

app.factory('answerService', function () {
    var answers = {
        1 : { text : "first answer" },
        2 : { text : "second answer" }
    }
    return {
        getAllAnswers: function () {
            return answers;
        },
        getFirstAnswer: function () {
            return answers[1];
        }
    }
});

In my controller, I initiate the first answer by calling the service to get the first answer.

app.controller('myController', function ($scope, answerService) {
    $scope.firstAnswer = answerService.getFirstAnswer();    
    $scope.answers = answerService.getAllAnswers();
});

http://jsfiddle.net/hientc/gj5knrg7/2/

The problem I'm having is that $scope.firstAnswer is somehow also being binded to $scope.answers. I don't want that, I only want the input to bind to scope.firstAnswer.text

What am I doing wrong?

ryeballar

This is because answers[1] is an object reference, and assigning its value to another variable signifies that the variable is a reference to that object. In order to get a copy of that value you can copy it using angular.copy().

Simply change getFirstAnswer() function to something like this:

DEMO

getFirstAnswer: function () {
  return angular.copy(answers[1]);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Initiating directive from controller

From Dev

how do i pass scope from controller to service in angularjs?

From Dev

Using AngularJS service to update scope in other controller from other module

From Dev

Angularjs service callback to update scope of controller

From Dev

AngularJS Bind service array variable to controller scope

From Dev

AngularJS - refactoring controller into service - understanding self/this and scope

From Dev

AngularJS Bind service array variable to controller scope

From Dev

How to Forward $scope from Controller to Controller AngularJS?

From Dev

angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

From Dev

Applying results from a factory service to a scope controller

From Dev

refresh specific controller $scope from service

From Dev

refresh specific controller $scope from service

From Dev

Pass $scope value from controller to a service function

From Dev

AngularJS - Access $scope from controller in custom filter

From Java

AngularJS access parent scope from child controller

From Dev

AngularJS access controller $scope from outside

From Dev

AngularJS from $scope get form controller programmatically

From Dev

Change controller scope value from directive AngularJS

From Dev

AngularJS from $scope get form controller programmatically

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

AngularJS Updating Scope from Service model

From Dev

angularjs bind service array length property to controller scope

From Dev

Not able to lazily update a controller scope with a REST service - Angularjs

From Dev

angularjs bind service array length property to controller scope

From Dev

Parent Controller's scope vs Shared Service in AngularJS?

From Dev

angularjs fill scope variable with REST service in existing controller

From Dev

AngularJS $scope.foo is set with service, but later on in the same controller undefined

From Dev

Call a method of a controller from another controller using 'scope' in AngularJS

From Dev

Accessing a service from a controller in another file in AngularJS

Related Related

  1. 1

    Initiating directive from controller

  2. 2

    how do i pass scope from controller to service in angularjs?

  3. 3

    Using AngularJS service to update scope in other controller from other module

  4. 4

    Angularjs service callback to update scope of controller

  5. 5

    AngularJS Bind service array variable to controller scope

  6. 6

    AngularJS - refactoring controller into service - understanding self/this and scope

  7. 7

    AngularJS Bind service array variable to controller scope

  8. 8

    How to Forward $scope from Controller to Controller AngularJS?

  9. 9

    angularjs - Watching service properties in a controller scope... changing them from a directive... no update?

  10. 10

    Applying results from a factory service to a scope controller

  11. 11

    refresh specific controller $scope from service

  12. 12

    refresh specific controller $scope from service

  13. 13

    Pass $scope value from controller to a service function

  14. 14

    AngularJS - Access $scope from controller in custom filter

  15. 15

    AngularJS access parent scope from child controller

  16. 16

    AngularJS access controller $scope from outside

  17. 17

    AngularJS from $scope get form controller programmatically

  18. 18

    Change controller scope value from directive AngularJS

  19. 19

    AngularJS from $scope get form controller programmatically

  20. 20

    AngularJS $watch controller variable from a directive with scope

  21. 21

    AngularJS Updating Scope from Service model

  22. 22

    angularjs bind service array length property to controller scope

  23. 23

    Not able to lazily update a controller scope with a REST service - Angularjs

  24. 24

    angularjs bind service array length property to controller scope

  25. 25

    Parent Controller's scope vs Shared Service in AngularJS?

  26. 26

    angularjs fill scope variable with REST service in existing controller

  27. 27

    AngularJS $scope.foo is set with service, but later on in the same controller undefined

  28. 28

    Call a method of a controller from another controller using 'scope' in AngularJS

  29. 29

    Accessing a service from a controller in another file in AngularJS

HotTag

Archive