AngularJS在同一个视图中使用两个控制器
AngularJS using two controllers in the same view
我正在尝试使用一个angularJS web应用程序。我买了一个模板,它的所有控制器,路由和指令在一个单一的文件app.js
这是我的第一个angularJS应用,或者实际上是我的第一个前端工作。现在正努力成为一个全栈极客…: D
所以这就是我现在卡住的地方。应用程序工作完全正常,但是当我在另一个文件auth.js
中添加一个新的控制器AuthCtrl
时,它给了我一个错误,说没有找到AppCtrl
。下面的HTML将使您更好地理解它。我已经指出了代码片段中具体的问题所在。
<!doctype html>
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>PropheSee Dashboard</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link href="http://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic" rel="stylesheet" type="text/css">
<!-- Needs images, font... therefore can not be part of main.css -->
<link rel="stylesheet" href="bower_components/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="bower_components/weather-icons/css/weather-icons.min.css">
<!-- end Needs images -->
<link rel="stylesheet" href="styles/main.css">
</head>
<body data-ng-app="app" id="app" class="app" data-custom-page="" data-off-canvas-nav="" data-ng-controller="AppCtrl" data-ng-class=" {'layout-boxed': admin.layout === 'boxed' } ">
<div data-ng-controller="AuthCtrl" data-ng-app="app">
<ul>
<li ng-repeat="phone in phones">
{{phone.name}}
<p>{{phone.snippet}}</p>
</li>
</ul>
</div>
<%-body%>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="scripts/vendor.js"></script>
<script src="scripts/ui.js"></script>
<script src="scripts/app.js"></script>
<!--
This is where the problem is. As soon as I add the following line to use AuthCtrl,
It says AppCtrl not found. Is it that I can't use more than one import for controllers? Or is there a problem in the way I am doing the imports?
-->
<script src="scripts/controllers/auth.js"></script>
</body>
Edit - AuthCtrl
var app = angular.module('app', []);
app.controller('AuthCtrl', function ($scope) {
$scope.phones = [
{'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.'},
{'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.'},
{'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.'}
];
});
<<p> App.js片段/strong> (function() {
"use strict";
angular.module("app.chart.ctrls",[])
.controller("chartCtrl",[
"$scope",
function($scope){ ....
问题是您在auth.js
文件中这样做:
var app = angular.module('app',.......
从而声明一个新的应用程序或覆盖在app.js
相反,你应该使用在app.js
文件中定义的相同的应用程序变量。
你需要声明应用程序,并在一个变量中存储对它的引用:
var yourApp = angular.module("app.controllers",[.........]);
然后要添加控制器,将它们赋值给"保存"应用定义的变量。在yourApp
应用程序上应该这样定义chartCtrl控制器:
yourApp.controller("chartCtrl",[
"$scope",
function($scope){ ....
然后你的auth.js文件中的控制器:
yourApp.controller('AuthCtrl', function ($scope) {
$scope.phones = [
{'name': 'Nexus S',
'snippet': 'Fast just got faster with Nexus S.'},
{'name': 'Motorola XOOM™ with Wi-Fi',
'snippet': 'The Next, Next Generation tablet.'},
{'name': 'MOTOROLA XOOM™',
'snippet': 'The Next, Next Generation tablet.'}
];
});
相关文章:
- 如何将json文件中的数据提取到对象数组中,并在两个控制器之间共享
- 如何在AngularJS页面中嵌套两个控制器
- 如何访问两个控制器AngularJS
- 可以't在相同的局部视图Angularjs中加载两个控制器
- 发布绑定/监视两个控制器之间共享的服务变量
- 来自两个控制器的角度连接数据
- AngularJS两个控制器..克服$scope$页面加载时打开
- AngularJS-在两个控制器之间共享数据,what'It’他错了
- 在两个控制器中共享的Angular JS Factory不会更新值
- 两个控制器,共同的工厂和重复刷新
- 从脚本调用两个控制器方法
- 如何在Angular中让两个控制器执行相同的操作,但改变站点的视觉效果
- AngularJS在同一个视图中使用两个控制器
- 两个控制器分别用于主应用和子应用,它们的url与angular ui-route相同
- Angular转换不能在两个控制器之间工作
- 如何在angularjs中的两个控制器之间共享$scope属性,而不是其值
- 在我的情况下,如何在两个控制器之间进行通信
- Angular使用Server-sent-events(EventSource)接收数据,并在两个控制器之间处理它
- 两个控制器,Angular中的一个作用域变量
- Ember.js:两个控制器之间的依赖关系失败