gpt4 book ai didi

javascript - 单击单选按钮时动态更改容器

转载 作者:行者123 更新时间:2023-11-30 20:57:10 25 4
gpt4 key购买 nike

我想在单击单选按钮后动态更改容器。我知道我可以使用 ng-model 和值轻松地做到这一点。这是一个 JSFiddle: http://jsfiddle.net/vDTRp/2/当您有一个小容器或只有一些文本时,它工作正常。但是我如何使用存储在作用域中的巨大容器来做到这一点?

function MyCtrl($scope) {
$scope.value[0]='<div>big container when clicking radiobutton 1</div>'
$scope.value[1]='<div>big container when clicking radiobutton 2</div>'
$scope.value[2]='<div>big container when clicking radiobutton 3</div>'
}

$scope.value[0] 得到了我的单选按钮 1 的容器,$scope.value[1] 得到了我的单选按钮 2 的容器,$scope.value[2] 得到了我的单选按钮 3 的容器。

HTML 应该是什么才能动态地执行此操作? (“动态地”是指单击单选按钮会更改容器,如 jsfiddle 中所示。)

非常感谢!

最佳答案

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
$scope.number = '0';
$scope.value = [];
$scope.value[0]='<div>big container when clicking radiobutton 1</div>';
$scope.value[1]='<div>big container when clicking radiobutton 2</div>';
$scope.value[2]='<div>big container when clicking radiobutton 3</div>';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<input type="radio" ng-model="number" value="0">
<input type="radio" ng-model="number" value="1">
<input type="radio" ng-model="number" value="2">
<hr>
{{value[number]}}
</div>
</div>

您可以像这样在选定的单选按钮上隐藏显示容器

<div ng-controller="MyCtrl">
<input type="radio" ng-model="number" value="0">
<input type="radio" ng-model="number" value="1">
<input type="radio" ng-model="number" value="2">
<hr>
{{value[number]}}
<div ng-bind-html-unsafe="value[number]"></div> <--if you want bind html than use ng-bind-html-unsafe
</div>

然后像这样改变你的 Controller

function MyCtrl($scope) {
$scope.number = '0';
$scope.value = [];
$scope.value[0]='<div>big container when clicking radiobutton 1</div>'
$scope.value[1]='<div>big container when clicking radiobutton 2</div>'
$scope.value[2]='<div>big container when clicking radiobutton 3</div>'
$scope.$watch('value', function(value) {
console.log(value);
});
}

关于javascript - 单击单选按钮时动态更改容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47529487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com