AngularJS:ng-click无法在自定义输入指令中工作

时间:2016-01-26 10:00:31

标签: angularjs angularjs-directive

我是AngularJS的新手,目前我正在使用带标签的自定义输入指令。
我在堆栈溢出中引用了一篇文章,但遇到了问题。

正在运行的代码是小提琴: http://jsfiddle.net/luneyq/bpN9b/18/

核心代码如下:

<!doctype html>
<html>
<head>
    <script src="../common/angular.js"></script>
 </head>
<body>

<div ng-app="myApp">
    <div ng-controller="MainController">
        <my-input type="number" name="valueNumber1" ng-model="valueNumber1" label="Age" ng-change="change()" ng-click="click()"></my-input>
        <div id="result">a</div>
    </div>
</div>

<script>
    var app = angular.module("myApp", []);

    app.controller('MainController', function($scope, $window){
        $scope.valueNumber1 = 10;
        $scope.change = function() {
            document.getElementById("result").innerHTML = 'change:' + $scope.valueNumber1;
        };
        $scope.click = function() {
            document.getElementById("result").innerHTML = 'click:' + $scope.valueNumber1;
        };
    });

    app.directive('myInput', function() {
        return {
            require:  '^ngModel',
            restrict: 'EA',
            scope: {
                ngModel: '=',
                name: '@name',
                label: '@label'
            },
            replace: true,
            transclude: true,
            //priority: 10,
            template: '<div>' +
            '<label for="{{ name }}">{{label}}</label>' +
            '<input id="{{ name }}" ng-model="ngModel" />' +
            '</div>',
            compile: function(tElement, tAttrs, transclude) {
                var tInput = tElement.find('input');
                // Move the attributed given to 'custom-input' to the real input field
                angular.forEach(tAttrs, function(value, key) {
                    if (key.charAt(0) == '$' || key == "class")
                        return;
                    tInput.attr(key, value);
                    tInput.parent().removeAttr(key);
                });
                tElement.removeAttr('ng-model');
                return;
            }
        };
    })
</script>

</body>
</html>

我的问题是:
1. ng-click和ng-change不在输入上工作
2.输入元素上的ng-model =“ngModel”,为什么在这里使用ngModel?如果我将ngModel更改为aaa,则输入的初始值消失 3.在编译功能中复制属性删除短划线( - )信号,将ng-click复制为ngClick。
我不确定这是导致这个问题的原因 任何人都可以帮忙解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你的问题是点击和更改方法不在你的隔离范围内 - 但是你没有在这里使用Angular的双向绑定 - 而只是在结果div上使用ng-bind - 这里是一个小提琴http://jsfiddle.net/bpN9b/20/

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

app.controller('MainController', function($scope, $window){
    $scope.valueNumber1 = 10;
});

app.directive('myInput', function() {
    return {
        require:  '^ngModel',
        restrict: 'EA',
        scope: {
            ngModel: '=',
            name: '@name',
            label: '@label'
        },
        replace: true,
        priority: 10,
        template: '<div class="cbay-input-div">' +
        '<label for="{{ name }}">{{label}}</label>' +
        '<input id="{{ name }}" ng-model="ngModel" />' +
        '</div>',
        compile: function(tElement, tAttrs, transclude) {
            console.log(tAttrs);
            var tInput = tElement.find('input');
            // Move the attributed given to 'custom-input' to the real input field
            angular.forEach(tAttrs, function(value, key) {
                //console.log(key + " = " + value);
                if (key.charAt(0) == '$' || key == "class")
                    return;
                tInput.attr(key, value);
                tInput.parent().removeAttr(key);
            });
            tElement.removeAttr('ng-model');
            return;
        }
    };
});

这是模板

<div ng-app="myApp">
    <div ng-controller="MainController">
        <my-input type="number" name="valueNumber1" ng-model="valueNumber1" label="Age" ng-change="change(this)" ng-click="click(this)"></my-input>
        <div id="result" ng-bind="valueNumber1">a</div>
    </div>
</div>