[英]How to set new value of the Angular directive “ng-init” depending on the value of a <select> dropdown box?
So I want to set a value for ng-init
if a specific option is selected. 因此,如果选择了特定选项,我想为
ng-init
设置一个值。 If that specific option is not selected yet, or is changed away from, I'd like to delete the ng-init
value. 如果尚未选择该特定选项,或者更改了该选项,我想删除
ng-init
值。
<select id="exSelect" onchange="checkButton()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<textarea type="text"
class="form-control"
ng-model="vm.request.description"
name="description" id="description"
rows="10" placeholder="Must be atleast 50 characters."
ng-minlength="50"
required>
</textarea>
function checkButton()
{
var dropdownValue = document.getElementById("exSelect");
if (dropdownValue.value == "3") {
//Why does this not work?
document.getElementById("description").ng-init = 'Example attribute setting';
} else {
document.getElementById("description").ng-init = '';
}
}
This code is super simplified example version of what I am working on right now, but overall a similar idea of what I am trying to do as a small part of a much larger program. 这段代码是我正在进行的工作的超简化示例版本,但总体上类似于我正在尝试做的更大程序的一小部分。 So I would like for the
checkButton()
function to run each time a new option is selected. 所以我想每次选择一个新选项时运行
checkButton()
函数。 If 3 is selected, I would like the if statement to execute and set the "ng-init" attribute of the element with the Id of "description" as the text in the function. 如果选择3,我希望if语句执行并设置元素的“ng-init”属性,其中Id为“description”作为函数中的文本。 If another option is selected, I would like for the
checkButton()
function to run again and it will set the the ng-init
attribute as empty. 如果选择了另一个选项,我希望
checkButton()
函数再次运行,并将ng-init
属性设置为空。
First use the ng-model
and ng-change
directives: 首先使用
ng-model
和ng-change
指令:
<select id="exSelect" ng-model="vm.sel1" ng-change="vm.checkButton()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Then in your controller: 然后在你的控制器中:
vm.checkButton = function() {
if (vm.sel1 == "3") {
vm.request.description = 'Example attribute setting';
} else {
vm.request.description = = '';
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.