答案 0 :(得分:0)
我实际上正在更新这个小提琴,我会发布完整而好的方法来做到这一点。 但是,就目前而言,这是我的答案:
<强> Html5的强>
<div id="bob" ng-app ng-controller="MyCtrl">
<select ng-model="ampm" ng-options="item.friendName for item in options[1]"></select>
AM/PM: {{ampm}}
<button ng-click="changeTopics()">Change</button>
<button ng-click="click()"></button>
</div>
<强>的Javascript 强>
function MyCtrl($scope, $compile) {
var selectElement = angular.element("<select ng-model='ampm' ng-options='item.friendName for item in options[1]' class='ng-pristine ng-valid'></select>");
var integer = 0;
$scope.click = function(){
selectElement = angular.element("<select ng-model=\"test"+ integer +"\" ng-options=\"item.friendName for item in options[" + integer + "]\" class='ng-pristine ng-valid'></select>");
htmlDiv = "<div id=\"test"+ integer +"\"></div>"
var string ='test'+integer;
console.log(integer);
div = document.getElementById('bob');
div.insertAdjacentHTML('beforeend', htmlDiv);
angular.element(document.getElementById('test'+integer)).append(selectElement);
integer++;
$compile(selectElement)($scope);
}
function removeElement(element) {
element && element.parentNode && element.parentNode.removeChild(element);
}
$scope.changeTopics = function(){
removeElement( document.getElementById("test"+ (integer-1)) );
delete $scope.options.splice(integer,1);
if(integer>0)
integer--;
console.log(integer);
}
$scope.ampm = 0;
$scope.options = [
[{
"friendName": "Escobar",
"points": "200"
}, {
"friendName": "Pedro",
"points": "200"
}],
[{
"friendName": "Cicero",
"points": "200"
}, {
"friendName": "Ciclano",
"points": "200"
}],
[{
"friendName": "Jeremias",
"points": "200"
}, {
"friendName": "Boça",
"points": "200"
}],
[{
"friendName": "Leite com pera",
"points": "200"
}, {
"friendName": "Rato borrachudo",
"points": "200"
}]
];
}
我得到了朋友的帮助,发现了$ compile。感谢。