带有多个控制器的 Angular 一个模块不工作



我是angular js的新手,是否可以在单个模块中创建多个控制器?

   
<body ng-app="birth" >
	<div ng-controller="learnerToSurvive" ng-show="$learn">
		<p>xxx</p>
		<button ng-click="start">Let's Begin</button>
	</div>
	<div ng-controller="startToSurvive" ng-show="$begin">
		<button ng-model="$CPU"> VS CPU </button>
		<button ng-model="$multiplayer"> Multiplayer</button>
		<div ng-show="$options">
			<label for="two">Two</label><input type="radio" id="two" ng-model="$players" name="multi" text="two"/>
			<label for="three">Three</label><input type="radio" id="three" ng-model="$players" name="multi" text="three"/>
			<label for="four">Four</label><input type="radio" id="four" ng-model="$players" name="multi" text="four"/>
		</div>
	</div>
	<div ng-controller="surviveBegins" ng-show="$play">
		<canvas id="survival" style="height: 300px, widht:500px">
	</div>
	<div ng-controller="death" ng-show="$over">
	</div>
</body>

这很容易用$rootScope实现,尽管不建议这样做。

为我的示例提供了一个 jsfiddle:http://jsfiddle.net/dn4e4j01/1/

在我看来,ui路由器将是一种更好的方法:

https://github.com/angular-ui/ui-router

是的。如果在 Javascript 文件中正确定义了这些控制器,应该没有问题。但是,根据您的示例,也许您需要像ui-router这样的东西才能从一种状态转到另一种状态。

最新更新