Bruno
Bruno

Reputation: 418

AngularJS display alert depending on input value

I am coding a CRUD application in Angular and Web API. In the create/edit form (same template), I need to display an error message if 2 input fields don't have the same number of comma ','.

I found a way to make it work but I don't like it, I think it needs to be "angularized" but I don't know how.

Controller:

$scope.isColumnCountOk = true;
$scope.checkColumnCountOk = function () { $scope.isColumnCountOk = AdminUtils.isColumnCountOk($scope.item.columnSqlNames, $scope.item.columnTitles) };

Template:

 <input type="text" class="form-control" ng-model="item.columnTitles" placeholder="Report Column 1,Report Column 2,Report Column 3,..." ng-blur="checkColumnCountOk()" required />
            <div ng-hide="isColumnCountOk">
                <br />
                <div class="alert alert-danger" ><strong>Warning. </strong>Mismatch between column numbers</div>
            </div>

I would like to get rid of the $scope.isColumnCountOk and have the function called directly from the template, without storing the return value in the scope.

Any advice?

Bruno

Upvotes: 0

Views: 904

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37520

I think both of these ways should work...

$scope.isColumnCountOk = function () { 
    return AdminUtils.isColumnCountOk($scope.item.columnSqlNames, $scope.item.columnTitles);
};

<div ng-hide="isColumnCountOk()">
    ...
</div>

Or

$scope.isColumnCountOk = AdminUtils.isColumnCountOk; 

<div ng-hide="isColumnCountOk(item.columnSqlNames, item.columnTitles)">
    ...
</div>

Upvotes: 1

Related Questions