简体   繁体   English

如何将controller.js包含在ng-include html页面中?

[英]how to include controller.js into ng-include html page?

this is home.html 这是home.html

 <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>SPM</title> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="../css/bootstrap/bootstrap.min.css" /> <link rel="stylesheet" type="text/css" href="../css/common/common.css" /> <link rel="stylesheet" type="text/css" href="../js/treeview/css/angular.treeview.css"> <script src="../js/jquery/jquery-2.1.1.min.js"></script> <script src="../js/bootstrap/bootstrap.js"></script> <script type="text/javascript" src="../js/angular/angular.min.js"></script> <script type="text/javascript" src="../js/treeview/angular.treeview.min.js"></script> <script type="text/javascript" src="../js/AJcontrollers/spmControllers.js"></script> <script type='text/javascript'>//<![CDATA[ //angular module 'use strict'; alert("1"); var myApp = angular.module('myApp', ['angularTreeview','ngLoadScript']); myApp.controller('commonController', ['$scope', '$http', function($scope, $http) { $scope.menus = [ { link : '/summary', treeState : '' , url: 'body.html'}, { link : '/indicator', treeState : '' , url: 'manageHierarchy.html'} ]; $scope.menu = $scope.menus[0]; }]); </script> </head> <body ng-controller="commonController"> <div> <div ng-include="'head.html'"></div> </div> <!-- footer --> <footer class="footer"> <div ng-include="'footer.html'"></div> </footer> <!-- footer --> </body> </html> 

and

head.html is head.html是

 <script type='text/javascript'> myApp.controller('headController', ['$scope', '$http', function($scope, $http) { $scope.change = function (param){ alert(param); }; }]); </script> <div ng-controller="headController"> details...... </div> 

use angularjs version 1.3.7 使用angularjs版本1.3.7

problem is 'can't find headController' 问题是'找不到headController'

in my code what shoud i do ? 在我的代码中,我该怎么办? can i change my angular version? 我可以改变我的角度版本吗? 1.2.X version can allow function headController($scope){ .....}; 1.2.X版本可以允许函数headController($ scope){.....}; but 1.3.7 not work what can i do ? 但是1.3.7无法正常工作我该怎么办?

This is a know issue that has been reported. 这是一个已报道的已知问题。

https://github.com/angular/angular.js/issues/3756 https://github.com/angular/angular.js/issues/3756

https://gist.github.com/endorama/7369006 https://gist.github.com/endorama/7369006

Long story short, the script tag is a special little flower and angular does not treat it as such. 长话短说,脚本标签是一个特殊的小花,角度不会这样对待它。 If you include jquery before you include angular this issue will resolve itself. 如果在包含angular之前包含jquery,则此问题将自行解决。 Otherwise you can do the script lazy loading technique that the github links recommend. 否则你可以执行github链接推荐的脚本延迟加载技术。

Or just move the script tag from head.html and put it on home.html since it will always be needed. 或者只是从head.html移动脚本标记并将其放在home.html上,因为它总是需要的。 (Disclaimer: I would not do this for an app of any size and complexity, but for something small it is the easiest solution. Of course creating js files and including them via seperate scripts would be best.) (免责声明:我不会为任何大小和复杂的应用程序执行此操作,但对于小的东西,它是最简单的解决方案。当然,创建js文件并通过单独的脚本包含它们将是最好的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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