What would be the most efficient way of conditionally changing the background color for a required input once it is valid using AngularJS 1.5.6? Per my understanding and what I have read online, I should avoid using $scope and instead use controllerAs. How would I refactor to the follow AngularJS code to use controllerAs?
Only CSS and HTML
HTML
<input id='textfield' placeholder='Search' required>
CSS
#textfield:valid {
background-color: green;
color: white;
}
Using $scope
HTML
<div ng-app="myApp" ng-controller="testController">
<input type="text" ng-class="cssClass" ng-change="changeCssInput()" ng-model="text"/>
</div>
CSS
.input-green {
background-color: green;
color: white;
}
.input-red {
border: 3px solid red;
}
AngularJS
angular.module('myApp', []).
controller('testController', function ($scope)
{
$scope.text = '';
$scope.cssClass = 'input-red';
$scope.changeCssInput = function () {
$scope.cssClass = $scope.text.length <= 0 ? 'input-red' : 'input-green';
}
});