ng-change,ng-click不工作仅在指令中选择 有谁可以帮助我?
HTML
<div select-list="items"></div>
指令:
myApp.directive("selectList", function(){
return function (scope, element, attr) {
var data = scope.items;
if(angular.isArray(data)){
var selectElem = angular.element("<select>").attr('ng-change', 'test()');
element.append(selectElem);
for(var i = 0; i < data.length; i++){
var optionElem = angular.element("<option>");
selectElem.append(optionElem.text(data[i].name));
}
}
}
});
控制器:
var myApp=angular.module('myApp', []);
myApp.controller('phoneController', function($scope) {
$scope.items = [
{ name: 'Nokia Lumia 630'
},
{ name: 'Xiaomi Mi5'
}
];
$scope.selectedItem = $scope.items[0].name;
$scope.select = function(name){
$scope.selectedItem = name;
};
$scope.test = function(){
alert("test");
};
});
我是新手。 Angular版本1.3 也许这取决于版本。我现在无法改变版本
答案 0 :(得分:0)
如果要插入带有角度绑定的元素,则需要使用$ compile函数。没有测试过这个,但它是正确的方向。
myApp.directive("selectList", function ($compile) {
function link(scope, element, attr) {
var data = scope.items;
if (angular.isArray(data)) {
var selectElem = $compile('<select ng-change="text()"></select>');
element.append(selectElem);
for (var i = 0; i < data.length; i++) {
var optionElem = angular.element("<option>");
selectElem.append(optionElem.text(data[i].name));
}
}
}
return {
link: link
};
});