我的代码是,
<form name="myForm" ng-controller="Ctrl"> <input type="radio" ng-model="color" value="red"> Red <br/> <input type="radio" ng-model="color" ng-value="specialValue"> Green <br/> <input type="radio" ng-model="color" value="blue"> Blue <br/> </form> <div id="reddiv">Red Selected</div> <div id="greendiv">Green Selected</div> <div id="bluediv">Blue Selected</div>
我的剧本是
function Ctrl($scope) { $scope.color = 'blue'; if ($scope.color == 'blue') { //blue div show } else if($scope.color == 'green') { //green div show } else { //red div show } }
Angular方式是使用ngShow / ngHide / ngIf指令来显示相应的div.考虑这个例子:
app.controller('Ctrl',function($scope) { $scope.color = 'blue'; $scope.isShown = function(color) { return color === $scope.color; }; });
HTML:
<div ng-show="isShown('red')">Red Selected</div> <div ng-show="isShown('green')">Green Selected</div> <div ng-show="isShown('blue')">Blue Selected</div>
演示:http://plnkr.co/edit/yU6Oj36u9xSJdLwKJLTZ?p=preview
同样非常重要的是ng-controller =“Ctrl”应该移动到你的表格之上,因为潜水应该在同一范围内.