AngularJS ng show指令不应该处理作用域函数
这个简单的ng show指令有什么问题?这应该很简单,因为我必须使用ng show隐藏按钮,当单击第三个按钮时,这两个按钮将显示,因此它们在单击按钮之前是隐藏的 javascriptAngularJS ng show指令不应该处理作用域函数,angularjs,Angularjs,这个简单的ng show指令有什么问题?这应该很简单,因为我必须使用ng show隐藏按钮,当单击第三个按钮时,这两个按钮将显示,因此它们在单击按钮之前是隐藏的 javascript $scope.addQuestion = function () { $('button#addQuestion').click(function(){ $scope.addQuestion = true; console.log("clicked")
$scope.addQuestion = function () {
$('button#addQuestion').click(function(){
$scope.addQuestion = true;
console.log("clicked");
})
}
HTML
添加
复制
问题
如果要修改Jquery函数中的作用域,必须手动调用$apply方法
$scope.addQuestion = function () {
$('button#addQuestion').click(function(){
$scope.addQuestion = true;
$scope.$apply();
console.log("clicked");
})
}
但是我非常确定,您不需要JQuery来完成您想要的任务 你有混搭。。你只需要这个:- 不再使用jquery click事件处理程序,只需添加处理程序函数,在作用域上设置变量:-
$scope.addQuestion = function () {
$scope.showQuestion = true;
}
和ng show=“showQuestion”
,并在触发器上单击ng=“addQuestion()”
<div class="program-edit-btns well">
<!--These two buttons are hidden until the third button is clicked-->
<div class="btn-group pull-left" ng-show="showQuestion">
<button type="button" class="btn btn-remove"><i class="fa fa-plus"></i> add</button>
<button type="button" class="btn btn-remove"><i class="fa fa-files-o"></i> copy</button>
</div>
<!--This button triggers the ng-show-->
<button id="addQuestion" ng-click="addQuestion()" type="button" class="btn btn-remove pull-left">
<i class="fa fa-plus"></i> question
</button>
</div>
添加
复制
问题
我已经试过了,但仍然没有显示其他两个按钮还有一个问题,如果我想在单击事件上添加一个类,我会这样添加:$scope.addQuestion=function(event){$scope.showQuestion=true;$(event.target).addClass('disabled');}@jmccommas您应该只使用angular来添加类,即使用ng class
并将其绑定到某个变量上,即在使用angular时尽量避免使用jquery处理程序。。在这里,您正在每个摘要周期注册一个单击事件。。。还请注意,作用域上的函数和作用域变量的名称相同…@jmccommas欢迎使用。。。希望你的问题得到解决@jmccommas你介意接受对你帮助最大的答案吗?
<div class="program-edit-btns well">
<!--These two buttons are hidden until the third button is clicked-->
<div class="btn-group pull-left" ng-show="showQuestion">
<button type="button" class="btn btn-remove"><i class="fa fa-plus"></i> add</button>
<button type="button" class="btn btn-remove"><i class="fa fa-files-o"></i> copy</button>
</div>
<!--This button triggers the ng-show-->
<button id="addQuestion" ng-click="addQuestion()" type="button" class="btn btn-remove pull-left">
<i class="fa fa-plus"></i> question
</button>
</div>