[英]ng-change is not working on select in my directive
ng-change, ng-click not working on select only in directive can anyone help me? ng-change, ng-click 不能只在指令中选择 任何人都可以帮助我吗?
HTML HTML
<div select-list="items"></div>
directive:指示:
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));
}
}
}
}); });
controller:控制器:
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");
};
}); });
im newbie here.我是新手。 Angular version 1.3 Maybe it depends on version. Angular 版本 1.3 也许这取决于版本。 I cant to change version now我现在不能更改版本
you need to use $compile function if you are inserting elements with angular bindings.如果要插入带有角度绑定的元素,则需要使用 $compile 函数。 Didn't test this, but it's the right direction.没有测试这个,但这是正确的方向。
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
};
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.