Angular Controller未触发

时间:2019-09-29 22:58:21

标签: angularjs

我的控制器没有触发。应该是愚蠢的,但我不明白为什么。

在这里找到我的代码结构。

请帮助我找到该基本错误。

模板:

<!DOCTYPE html>
<html ng-app="attachmentsApp" ng-cloak="">
<head>
  <meta charset="utf-8"/>


  <script th:inline="javascript">
/*<![CDATA[*/
var source = [[${source}]];
var appContext = /*[[@{/}]]*/ '';
/*]]>*/
</script>

</head>
<body>

<div th:replace="include"></div>
<script th:src="@{/app/modules/attachments/scripts/attachmentsapp.js}"></script>
<script th:src="@{/app/modules/attachments/scripts/controllers/AttachmentsHomeCtrl.js}"></script>
<script th:src="@{/app/modules/attachments/scripts/services/AttachmentsHomeService.js}"></script>
</body>
</html>

应用程序:

angular
  .module('attachmentsApp', ['ngRoute'
    ]).run(['$rootScope', function($rootScope){
        $rootScope.appContext = window.appContext;
        $rootScope.language = window.language;
        $rootScope.source = window.source;
    }])
    .config(function($routeProvider) {
        $routeProvider.when('/', { templateUrl: window.appContext + 'app/modules/attachments/views/home.html', controller: "AttachmentsHomeCtrl" })
    });

控制器:

angular.module('attachmentsApp').controller('AttachmentsHomeCtrl', ['$scope','$rootScope','AttachmentsHomeService','$window', 
                                      function ($scope,$rootScope,AttachmentsHomeService,$window) {
    console.log($rootScope.source);
    debugger;
}]);

服务:

'use strict';
angular.module('attachmentsApp').service('AttachmentsService', [ '$http', '$rootScope', function($http, $rootScope){
    this.getForm = function(type) {
        debugger;
    }
    return this;
}]);

非常感谢

2 个答案:

答案 0 :(得分:0)

建议您对HTML进行一些更改:

1)在使用之前定义和加载文件:在attachmentsapp.js文件加载到DOM中之前已使用ng-app

2)使用顺序:服务文件在控制器中使用后已加载

应为:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8"/>


      <script th:inline="javascript">
    /*<![CDATA[*/
    var source = [[${source}]];
    var appContext = /*[[@{/}]]*/ '';
    /*]]>*/
    </script>
    <script th:src="@{/app/modules/attachments/scripts/attachmentsapp.js}"></script>

<script th:src="@{/app/modules/attachments/scripts/services/AttachmentsHomeService.js}"></script>

<script th:src="@{/app/modules/attachments/scripts/controllers/AttachmentsHomeCtrl.js}"></script>

    </head>
    <body ng-app="attachmentsApp" ng-cloak="">

    <div th:replace="include"></div>

    </body>
    </html>

不能100%确定,但是应该可以。试试吧!

此外,请注意控制台。

答案 1 :(得分:0)

这是服务名称。

AttachmentsHomeService在控制器中注册 服务定义中的AttachmentsService

非常感谢您的帮助