0

I'm new in AngularJS, currently I'm working on custom input directive with a label.
I refer to one article in stack overflow but encountered problem.

The running code is on fiddle: http://jsfiddle.net/luneyq/bpN9b/18/

Core code is as below:

<!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>

My problems are:
1. ng-click and ng-change not working on the input
2. ng-model="ngModel" on input element, why ngModel is used here? if I change ngModel to aaa, the initial value for the input disappeared
3. attributes copy in compile function removed the dash(-) signal, ng-click is copied as ngClick.
I'm not sure is this the cause of this problem.
Can anyone help on this problem?

Lune
  • 241
  • 1
  • 3
  • 13

1 Answers1

0

Your problem was click and change methods not in your isolate scope - BUT you are not making use of Angular's two way binding here - instead simply use ng-bind on your result div - here's a fiddle 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;
        }
    };
});

here's the template

<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>
A Macdonald
  • 814
  • 1
  • 7
  • 10
  • I didn't catch your meaning, you add
    change="change" type="number"
    – Lune Jan 26 '16 at 10:35
  • sorry ignore the change and click attributes - I started addressing your original scope problem and they were to set the functions within your isolate scope which you would do by adding click: '=', change: '=' - BUT as I said this way of doing it is all wrong - just use ng-bind as per the fiddle :) – A Macdonald Jan 26 '16 at 10:38
  • Fair enough if that's how you prefer, but using jquery to update dom elements when you have automatic 2 way binding available with ngModel and ngBind is just making hard work and unnecessary code for yourself. – A Macdonald Feb 01 '16 at 07:47