Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng更改不适用于我的指令中的select_Javascript_Angularjs_Angularjs Ng Change - Fatal编程技术网

Javascript ng更改不适用于我的指令中的select

Javascript ng更改不适用于我的指令中的select,javascript,angularjs,angularjs-ng-change,Javascript,Angularjs,Angularjs Ng Change,ng更改,ng单击不处理指令中的仅选择 有人能帮我吗 HTML })) 我是这里的新手。 角度版本1.3 也许这取决于版本。我现在无法更改版本如果插入带有角度绑定的元素,则需要使用$compile函数。没有测试这个,但方向正确 myApp.directive("selectList", function ($compile) { function link(scope, element, attr) { var data = scope.items; if

ng更改,ng单击不处理指令中的仅选择 有人能帮我吗

HTML

}))

我是这里的新手。 角度版本1.3
也许这取决于版本。我现在无法更改版本

如果插入带有角度绑定的元素,则需要使用$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
    };
});
myApp.directive(“selectList”,函数($compile){
功能链接(范围、元素、属性){
var数据=范围项目;
if(角度isArray(数据)){
var selectElem=$compile(“”);
元素。追加(selectElem);
对于(变量i=0;i
请你把问题解释清楚一点好吗。乍一看,您正在指令代码中动态添加ng change。不能这样绑定。如何动态添加ng更改?
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");
};
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
    };
});