-2

I have following code in which I have to call RemoveItem() function on button click , But RemoveItem() function not defined error is occurred

JavaScript Code

$scope.AddShoppingItems = function() {
     console.log('clicked add item');

   i++;
         var div= document.createElement('div');
            div.innerHTML = 'Item : <input type="text" name="ShopItem_'+i+'"><input type="button" value="-" onclick="RemoveItem(this)">';
    document.getElementById('AddItemTextbox').appendChild(div);


    }
 $scope.RemoveItem = function(div){
 console.log('-ve clicked');
 document.getElementById('AddItemTextbox').removeChild(div.parentNode);
 i--;
 }
5
  • 2
    You should not be manipulating the raw DOM to begin with in Angular, you should be using Angular directives in templates to show or hide the button as needed. Commented Apr 8, 2017 at 8:37
  • so what is the exact error ? @deceze Commented Apr 8, 2017 at 8:39
  • The immediate problem is that you're trying to call an Angular scope method from plain JavaScript. The bigger problem is that your approach to this button is completely un-angularistic. Commented Apr 8, 2017 at 8:41
  • actually i want to add textbox and button dynamically by clicking on "AddItemButton" Commented Apr 8, 2017 at 8:43
  • Trivial problem if you spend 2 hours reading documentation and tutorials. In any case deceze already answered you problem. Commented Apr 8, 2017 at 9:06

2 Answers 2

0

You cannot simply append an html element to a dom aw what you did. You need to compile the html element to your scope then only your events in scope will trigger.

Try this.

$scope.AddShoppingItems = function () {
    console.log('clicked add item');

    i++;
    var div = document.createElement('div');
    div.innerHTML = 'Item : <input type="text" name="ShopItem_' + i + '"><input type="button" value="-" ng-click="RemoveItem(this)">';
    var temp = $compile(div)($scope);
    angular.element(document.getElementById('AddItemTextbox')).append(temp);

}
$scope.RemoveItem = function (div) {
    console.log('-ve clicked');
    document.getElementById('AddItemTextbox').removeChild(div.parentNode);
    i--;
}

Look here for more details. You cannot simply bind onclick event as like you did. Use ng-click instead

Sign up to request clarification or add additional context in comments.

Comments

0

Just replace this line

div.innerHTML = 'Item : <input type="text" name="ShopItem_'+i+'"> <input type="button" value="-" onclick="RemoveItem(this)">';
';
document.getElementById('AddItemTextbox').appendChild(div);

By

div.innerHTML = 'Item : <input type="text" name="ShopItem_'+i+'"><input type="button" value="-" ng-click="RemoveItem($this)">
document.getElementById('AddItemTextbox').appendChild($compile(div)($scope));

1 Comment

It will not help too. A little closer to proper solution but still not going to work because ng-click doesn't mean anything at all, unless compiled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.