0

I'm writing a small Angular app which relies on some common functions defined outside the app to do tasks across different controllers. In one of my controllers the scope object is declared when the controller is loaded. I'd like to be able to assign it later in one of my helper functions, but the assignment is not working.

Here's the relevant code:

.controller('myCtrl', function($scope, myFactory){

    $scope.myObj;

    $scope.saveEntry = function() {
        myFactory.saveEntry($scope.obj);
      formatSingleTableEntry($scope.obj, $scope.myObj);
      $scope.obj = {};
    }

  });

  // Entry Formatter
  function formatSingleTableEntry(entry, scopeObject) {
      if (typeof scopeObject === 'undefined') {
        scopeObject = [];
      }
      if (typeof entry.obsDt === 'string') {
          entry.obsDt = dateFormat(entry.obsDt);
      }
      scopeObject.push(entry);
    }

    // Date Formatter
    function dateFormat(date) {
      date = date.split('');
      date[10] = 'T';
      date = date.join('');
      return Date.parse(date);
    }

If I assign the scope object as an empty array in the controller, everything works, but assignment doesn't in this configuration. I can check to see that assignment actually happens in the helper function, but when I check in the controller it hasn't.

Why?

4
  • The way the code currently functions is the first entry that's saved will cause the helper function to assign the scopeObject variable as an array, then push the entry. I can check that, it works fine, then when I come back to the controller the scopeObject remains undefined even after assignment and the push. Commented Oct 7, 2015 at 22:55
  • explain come back in more detail....each use is a new instance. If you need session persistence data needs to be stored in service Commented Oct 7, 2015 at 22:56
  • Just doing console.logs, checking at the different points in the code what the type of the object is. That's come back. Commented Oct 7, 2015 at 22:57
  • Why not move those function inside the controller? Or make a service that performs those tasks? I am also wondering if you need to use $timeout() or $scope.$evalAsync() to start the digest process with the updates you did. Commented Oct 7, 2015 at 23:34

1 Answer 1

1

One main problem is while undefined $scope.myObj; is a primitive. WHen you pass that as argument to function there is no reference back to the original variable

var foo;

function test(myVar){
    myVar = 'Some string';
    //   myVar != foo
}

test(foo);
console.log(foo)// undefined

If the original variable is an array or object you pass a reference to that array or object and can thus manipulate it.

In short, just declare $scope.myObj =[]; and don't reassign it and break any reference created

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

3 Comments

I wanted to avoid returning the array, it felt redundant to reassign every time an entry was added, but basically this would be the only way using this helper function? That or assignment within the controller?
just make it empty array to start with and don't break reference by reassigning it
Right, I'm being obsessive. I'm using ng-show to hide the table this array will populate until there's data in the array. The empty array is truthy, so the table shows, which is why I wanted to put off assignment until later. I can use myObj[0] to hide the table until there's an actual entry, but like I said I'm being obsessive. I'll probably just do it that way... Thank you!

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.