0

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
4
  • 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) Commented Aug 27, 2013 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. Commented Aug 27, 2013 at 21:10
  • 1
    any reason you're not just creating angular services for these services and using DI? Commented Aug 27, 2013 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? Commented Aug 28, 2013 at 0:11

1 Answer 1

4

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

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

2 Comments

Exactly what I was going to say about the one EntityManager: (function () { // some code here }()), - not the one you have commented
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".

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.