Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have an angular function in my controller and I want to call an external javascript function, this won't work and I'm not sure what I'm missing:

$scope.addSubscription = function(){
  var id = $scope.userId;
  var name = Exec.UserService.fetch(id);
  $scope.user.push({id:id, name:name});
}

My external javascript code looks something like this:

var Exec = (function() {
  "use strict";
        return {
           EntityManager: (function(){
             // some code here
           }());

           UserService: (function(){
             return {
               fetch: function(id){
                 // some code here
                 return 'something';
               },

               modify: function(id, status){
                // some code here
               }
             }; 
           }()) 
         } // end of outer return
      }()); // end of Exec
share|improve this question
1  
Can you create a fiddle containing a self contained example of the problem ? (At first glance, I don't see why it wouldn't work) –  Benjamin Gruenbaum Aug 27 '13 at 20:50
    
This may be missing the point--I'm not sure if this file is required to be external to the Angular application--but if not, you could refactor the external Javascript code into an Angular service, then inject it into the necessary controllers. –  drdalton Aug 27 '13 at 21:10
1  
any reason you're not just creating angular services for these services and using DI? –  doogle Aug 27 '13 at 21:23
    
So I decided to create a service containing both EntityManager and UserService and it worked. But now my problem is trying to call the EntityManager.fetch function from UserService. I tried Exec.EntityManager.fetch among other things. It says it's undefined, why can't it see it? –  user462349 Aug 28 '13 at 0:11

1 Answer 1

The reason why it doesn't work is because you need to use ',' instead of ';' to separate the key-value pair in the object. It's a syntax error and you should be able to see it in the console of your browser.

var Exec = (function () {
    "use strict";
    return {
        EntityManager: (function () {

        }()),  // -> should be , not ;

        UserService: (function () {
            return {
                fetch: function (id) {
                    return 'something';

                },

                modify: function (id, status) {

                }
            };
        }())
    }
}());

DEMO

share|improve this answer
    
Exactly what I was going to say about the one EntityManager: (function () { // some code here }()), - not the one you have commented –  Mark Schultheiss Aug 27 '13 at 21:23
    
@MarkSchultheiss Oh thx. Updated. I am kinda slow today... –  zsong Aug 27 '13 at 21:24
    
sorry I wasn't able to copy paste the code on here so I wrote it out, but it does have the comma instead of semicolon. In the console I'm getting and "Error: Exec is undefined". –  user462349 Aug 27 '13 at 22:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.