【问题标题】:how to include controller.js into ng-include html page?如何将 controller.js 包含到 ng-include html 页面中?
【发布时间】:2014-12-22 08:39:20
【问题描述】:

这是 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>

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>
	

使用 angularjs 版本 1.3.7

问题是“找不到 headController”

在我的代码中我应该做什么? 我可以更改我的角度版本吗? 1.2.X版本可以允许函数headController($scope){ .....}; 但 1.3.7 不起作用 我该怎么办?

【问题讨论】:

标签: javascript angularjs controller


【解决方案1】:

这是一个已报告的已知问题。

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

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

长话短说,script标签是一朵特殊的小花,angular不这么对待。如果在包含 Angular 之前包含 jquery,此问题将自行解决。否则,您可以执行 github 链接推荐的脚本延迟加载技术。

或者只是将脚本标签从 head.html 移到 home.html 上,因为它总是需要的。 (免责声明:我不会为任何大小和复杂性的应用程序执行此操作,但对于较小的应用程序,这是最简单的解决方案。当然,创建 js 文件并通过单独的脚本包含它们是最好的。)

【讨论】:

  • 谢谢,我解决了这个问题......使用这段代码 App.config(function ($controllerProvider) { App.controller = $controllerProvider.register; });
  • @Chawinter 谢谢!您的评论帮助我让它工作..您应该将其添加为答案,然后将其标记为“已接受的答案”,以免有人错过这个宝石。
  • 两个小时...两个该死的小时。谢谢,您的评论帮助了@Cha winter
猜你喜欢
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
相关资源
最近更新 更多