Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - dynamically set $scope variable

I am new to angular and am trying to do something pretty simple in vanilla javascript but am unable to find out how to do it properly in angular!

I want to set up a general function to clear an input text field:

I have this html:

<input type="text" ng-model="css" ng-focus="clearInput('css')"/>

(got the ng-focus thing here: https://groups.google.com/forum/?fromgroups=#!topic/angular/A5Lyx8m3S4M it works well!)

and this function in my controller

$scope.clearInput = function (val) {
 $scope.val  = "";
};

Clearly this would work if I would use $scope.css, but I want to be able use the function for other input fields as well. There's got to be a nice angular way of doing this!!

Thanks!

Heading

like image 455
Craig Morgan Avatar asked Jul 19 '26 10:07

Craig Morgan


1 Answers

I think it's easier just to clear it using "css=''".

For example, the following clears the input each time you click on it:

<input type="text" ng-model="css" ng-click="css=''"></input>
like image 177
Andy Hayden Avatar answered Jul 22 '26 21:07

Andy Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!