javascript - 内部 JS Angular Controller 没有被调用

标签 javascript html angularjs

我已经在 HTML 的脚本标记中编写了一个 Controller 。当我运行代码时,代码会执行到依赖注入(inject),但回调函数不会执行。

当类似的代码在我开发的其他应用程序上成功运行时,我无法弄清楚这有什么问题。

此外,如果我显式调用回调函数,则无法识别注入(inject)的依赖项。

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

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

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

如果显式调用该函数,则不会识别上面注入(inject)的作用域。

最佳答案

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.

您需要做的第一件事是确保所有模块依赖项拼写正确,然后确保加载所有文件。

关于javascript - 内部 JS Angular Controller 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36542213/

相关文章:

javascript - ExtJS 4加载满足一定条件的列

javascript - setTimeout 与 setInterval - 使用的最佳实践

javascript - 单击按钮时是否可以调用多个方法?

html - 将 System.Windows.Controls.WebBrowser 的内容设置为静态 HTML 文字?

html - 列表中的某些超链接不可点击

javascript - 将 $scope 放在 Cytoscape 点击事件中

javascript - 如何将 html 文件加载到文章内容中?

用于 Kendo UI 移动设备的 AngularJS 路由

javascript - 使用工厂内部路由 Controller

html - 在 Chrome 上打印 Bootstrap 表时出现错误