简体   繁体   中英

How to access regular Javascript function from AngularJS controller

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM