Sergey Romanov
Sergey Romanov

Reputation: 3080

Angularjs does not work ng-click

My setup is very simple

<button ng-click="query = ''" ng-if="query">X</button>
<input type="text" ng-model="query">

Check on planker but when I click on the button input is not cleaned

http://plnkr.co/edit/RTIGXFHeth4EN6GnSt8Z?p=preview

Upvotes: 0

Views: 137

Answers (1)

Chandermani
Chandermani

Reputation: 42669

ng-if creates a child scope and the ng-click ="query=''" is set on child scope. Whereas your ng-model=query is defined on the parent scope.

The options are either to use ng-show

or pass a object property. See my fiddle here

http://plnkr.co/edit/LGmPALg4wTTmCJLE4aFa?p=preview

It would look like this

<body ng-app="" ng-init='query={data:""}'>
  <button ng-click="query.data = 'a'" ng-if="query.data">X</button>
    <input type="query" ng-model="query.data">
</body>

Also spend some time of understanding the prototype inheritance in angularjs scope https://github.com/angular/angular.js/wiki/Understanding-Scopes

Upvotes: 1

Related Questions