0

I've written a controller in the script tag in my HTML. When I run the code, the code is executed till the dependency injection, but the callback function is not executed.

I'm not able to figure out what is wrong with this when a similar code runs successfully on other apps I've worked on.

Also, if I call the callback function explicitly then the dependencies injected are not identified.

<script type="text/javascript">
    angular.module('DesignPortal.layout', [])
        .controller('NgLayoutController', NgLayoutController);

    NgLayoutController.$inject = ['$scope'];

    function NgLayoutController($scope) {
        var loggedUserId = dpConfig.userInfo.id;
    }
</script>

The scope injected above is not identified if the function is called explicitly.

3 Answers 3

1

It's Working on my plunk.

<script type="text/javascript">
    angular.module('DesignPortal.layout', [])
        .controller('NgLayoutController', NgLayoutController);

    NgLayoutController.$inject = ['$scope'];

    function NgLayoutController($scope) {
      alert("Its working!");
        var loggedUserId = dpConfig.userInfo.id;
    }
</script>

This happens due to a number of reasons such as removing a script for index.html for a module but leaving in the module dependency or even misspelling a module dependency.

The first thing you need to do is make sure all modules dependencies are spelled right, then make sure all files are loaded.

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

1 Comment

Yes, It does. The thing which I was not doing was that I wasn't including the controller on the HTML with the ng-controller directive. I had the understanding that inclusion in HTML is not required. But, apparently I was wrong.
1

Do you have some error in the console? It might be that you are loading your script before angular is loaded. Try to wrap your code in the function and place some breakpoints/debugs/console.log's to test at which point your code gets executed.

(function(){
  "use strict";

  angular.module('DesignPortal.layout', [])
      .controller('NgLayoutController', NgLayoutController);

  NgLayoutController.$inject = ['$scope'];

  function NgLayoutController($scope) {
      var loggedUserId = dpConfig.userInfo.id;
  }
})();

1 Comment

I've taken care of such things, and no there's no error in the console.
0

Try this one. I have not tried my self but it should work.

<script type="text/javascript">
    angular.module('DesignPortal.layout', [])
        .controller('NgLayoutController',['$scope', function($scope) {
            function NgLayoutController() {
                var loggedUserId = dpConfig.userInfo.id;
            }
        }]);    
</script>

Comments

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.