【发布时间】:2019-01-27 04:25:05
【问题描述】:
我正在使用 jquery 的 angularjs 应用程序,似乎无法更改某些输入的背景颜色。简而言之,当用户单击“隐藏舞台”文本或“取消隐藏舞台”文本时,输入框背景应分别变为白色/浅灰色。但唯一改变的是“隐藏阶段”到“取消隐藏阶段”之间的文本,这很好。这是angularjs文件:
function stages($scope,$rootScope,$apiSrvc,$compile){
// defintions
// setting up stages statuses to angular array.
$scope.stages_visibilities = ['unhidden','unhidden','unhidden','unhidden','unhidden','unhidden','unhidden','unhidden'];
// changes the stage's status and it's visibility settings
$scope.setStageStatus = function(stageN,status){
$scope.stages_visibilities[stageN] = 'hidden';
if(status === 'unhide'){
$scope.stages_visibilities[stageN] = 'unhidden';
}
$scope.showStages();
};
// shows the stage rows.
$scope.showStages = function() {
// update(populate) the caption/value rows
$('#stages_rows').empty();
$nStages = parseInt($scope.nStages);
var stage_row = "";
for(var i=0; i < $nStages; i++){
stage_row += '<div class="stage_row">'+
'<input type="text" id="stage_caption_'+i+'" class="stg_caption" />'+
'<input type="number" id="stage_value_'+i+'" class="stg_val" />';
// show hide/unhide for this stage.
if($scope.stages_visibilities[i] === 'unhidden'){
stage_row += '<span id="stage_hide_unhide_'+i+'" class="hide_unhide_stg" ng-click="setStageStatus('+i+',\'hide\')">hide stage</span>';
// set row color to white.
$("#stage_caption_"+i).css("background-color","white");
$("#stage_value_"+i).css("background-color","white");
$("#stage_value_"+i).prop('disabled', false);
$("#stage_caption_"+i).prop('disabled', false);
}
else {
stage_row += '<span id="stage_hide_unhide_'+i+'" class="hide_unhide_stg" ng-click="setStageStatus('+i+',\'unhide\')">un-hide stage</span>';
// set row color to lightgrey.
$("#stage_caption_"+i).css("background-color","lightgrey");
$("#stage_value_"+i).css("background-color","lightgrey");
$("#stage_value_"+i).prop('disabled', true);
$("#stage_caption_"+i).prop('disabled', true);
}
stage_row += '</div>';
}
$("#stages_rows").append(stage_row);
// register new directives to angularjs
$compile($("#stages_rows").contents())($scope);
}
}
所有有角度的 js 初步工作都可以正常工作,只是代码的那一部分不会改变 $scope.showStages 函数中的输入(#stage_caption_i 和 #stage_value_i)的背景颜色。我不知道出了什么问题。
【问题讨论】:
-
请创建一个Minimal, Complete, and Verifiable Example。通读代码并理解每一行的作用太难了。
-
像这样将 AngularJS 与 jQuery 混合是自找麻烦。使用 AngularJS 框架,控制器不应该操纵 DOM。 DOM 操作应该在指令中完成。见“Thinking in AngularJS” if I have a jQuery background。
标签: javascript jquery css angularjs