在angularjs中将ng-click值初始化为true
Initialize ng-click value as true in angularjs
我正在使用一个角度的水疗中心,在那里我使用 ng 单击在页面之间切换(通过显示和隐藏div)。一切正常,这是我正在使用的代码的简化版本:
<a href="" ng-click="first=true; second=false; third=false">First</a>
<a href="" ng-click="first=false; second=true; third=false">Second</a>
<a href="" ng-click="first=false; second=false; third=true">Third</a>
<div class="container" id='settings' ng-show='first'> First </div>
<div class="container" id='settings' ng-show='second'> Second </div>
<div class="container" id='settings' ng-show='third'> Third </div>
但是,最初不显示任何div - 因为没有单击任何链接。我希望第一页最初显示,然后在单击另一个链接时隐藏。我该怎么做?
我想提出一些不同的解决方案组合ng-repeat
和ng-switch
指令,非常适合您的情况
法典
$scope.items = [
{id: 1, name: 'First'},
{id: 2, name: 'Second'},
{id: 3, name: 'Third'},
];
$scope.model = { selected: 1};
.HTML
<body ng-controller="MainCtrl">
<a href="" ng-click="model.selected = item.id" ng-repeat="item in items">
{{item.name}}
</a>
<div class="container" id='settings' ng-switch='model.selected'>
<div ng-switch-when="1"> First </div>
<div ng-switch-when="2"> Second </div>
<div ng-switch-when="3"> Third </div>
</div>
</body>
演示普伦克
在ng-init
中定义first=true
,这样就不必更改控制器。但是,我建议您在controller
中初始化它
另请注意,您必须将其他scope
变量定义为控制器中的false
变量,因为现在它们正在undefined(falsey)
这是代码按预期执行的原因,但定义它将使其更具可读性。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.first = true;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<a href="" ng-click="first=true; second=false; third=false">First</a>
<a href="" ng-click="first=false; second=true; third=false">Second</a>
<a href="" ng-click="first=false; second=false; third=true">Third</a>
<div class="container" id='settings' ng-show='first'>First</div>
<div class="container" id='settings' ng-show='second'>Second</div>
<div class="container" id='settings' ng-show='third'>Third</div>
</div>
使用ng-init
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl" ng-init="first = true">
<a href="" ng-click="first=true; second=false; third=false">First</a>
<a href="" ng-click="first=false; second=true; third=false">Second</a>
<a href="" ng-click="first=false; second=false; third=true">Third</a>
<div class="container" id='settings' ng-show='first'>First</div>
<div class="container" id='settings' ng-show='second'>Second</div>
<div class="container" id='settings' ng-show='third'>Third</div>
</div>
您需要在视图或控制器中初始化它,这是在视图中执行此操作的方法
<body ng-controller="MainCtrl" ng-init="first=true">
<a href="" ng-click="first=true; second=false; third=false">First</a>
<a href="" ng-click="first=false; second=true; third=false">Second</a>
<a href="" ng-click="first=false; second=false; third=true">Third</a>
<div class="container" id='settings' ng-show='first'> First </div>
<div class="container" id='settings' ng-show='second'> Second </div>
<div class="container" id='settings' ng-show='third'> Third </div>
</body>
但是正如注释和 Angular 文档所建议的那样,您应该更喜欢在控制器中初始化它。
app.controller('MainCtrl', function($scope) {
$scope.first = true;
});
<body ng-controller="MainCtrl">
<a href="" ng-click="first=true; second=false; third=false">First</a>
<a href="" ng-click="first=false; second=true; third=false">Second</a>
<a href="" ng-click="first=false; second=false; third=true">Third</a>
<div class="container" id='settings' ng-show='first'> First </div>
<div class="container" id='settings' ng-show='second'> Second </div>
<div class="container" id='settings' ng-show='third'> Third </div>
</body>
相关文章:
- 使用javascript函数在页面初始化后加载jquery
- 未捕获错误:无法在初始化之前调用方法;
- 如何在剑道网格初始化后设置pageSizes
- React redux初始化功能,无论状态变化如何
- 为什么可以't Protractor在自动初始化的Angular网站上查找Angular
- 尝试初始化()Spine's控制器和故障
- Angular.js延迟控制器初始化
- 对插件初始化后动态加载的元素进行样式设置
- Angular JS控制器初始化错误
- 在XMLHttpRequest之后重新初始化jQuery
- ember-js组件初始化不同的函数
- Youtube JavaScript API不是't正在初始化
- 函数无法识别初始化的变量
- datepicker引导程序再次初始化
- jquery数据表在初始化时设置宽度
- 如何使用app和secret进行安全的解析初始化
- 从javascript中再次初始化c变量
- ng重复中的ng模型-初始化唯一作用域属性
- 正在Ajax调用上初始化主干视图
- 用真或假初始化变量是否在 Javascript 中建立了内容的方向