Question

I am new to AngularJS and am trying to figure out how to show an HTML element based on a variable value.

I have tried this:

<div id="checkoutForm" class="form" ng-show="$scope.matchCount==1">
</div>

and this:

<div id="checkoutForm" class="form" ng-show="matchCount==1">
</div>

Here is my code so far:

<div id="visitorForm" class="form" ng-controller="VisitorLogController">
    <input id="firstNameTb" ng-model="formData.FIRSTNAME" placeholder="@Model.FirstNameStr" ng-blur="findUser()" class="form-control" value="{{FIRSTNAME}}"/>
    <input id="lastNameTb" ng-model="formData.LASTNAME" placeholder="@Model.LastNameStr" ng-blur="findUser()" class="form-control" value="{{LASTNAME}}" />
    <input id="companyTb" ng-model="formData.COMPANYNAME" placeholder="@Model.CompanyStr" ng-blur="findUser()" class="form-control" value="{{COMPANYNAME}}" />
    <input id="codeTb" ng-model="formData.CHECKCODE" placeholder="@Model.CodeString" title="@Model.CodeStringDesc" ng-blur="findUser()" class="form-control" />
</div>

<div id="checkoutForm" class="form" ng-show="matchCount==1">
    <h3>@Html.Raw(@Model.RecordFoundStr)</h3>
</div>

And in my .js file:

function VisitorLogController($scope, $http) {

    $scope.matchCount = null;
    $scope.findUser = function () {
        $http({
            method: 'POST',
            url: rootUrl + "VisitorLog/Check",
            data: $.param($scope.formData),
            headers: { 'Content-type': 'application/x-www-form-urlencoded' }
        }).success(function (data) {
            $scope.matchCount = data.count;
            if (data.count == 1) {
                $scope.FIRSTNAME = data.visitors[0].FirstName;
                $scope.LASTNAME = data.visitors[0].LastName;
                $scope.COMPANYNAME = data.visitors[0].CompanyName;
            }
            $scope.message = data.message;
        });
    }
}

The function fires as expected and I can see the count and verify that $scope.matchCount is correctly getting updated with the record count, but it does not affect the UI at all.

What am I missing?

Was it helpful?

Solution

The div with ng-show="matchCount==1" is outside the scope of VisitorLogController.

Moving the ng-show within the scope should make it work.

<div ng-controller="VisitorLogController">
    <div id="visitorForm" class="form">
        <input id="firstNameTb" ng-model="formData.FIRSTNAME" placeholder="@Model.FirstNameStr" ng-blur="findUser()" class="form-control" value="{{FIRSTNAME}}"/>
        <input id="lastNameTb" ng-model="formData.LASTNAME" placeholder="@Model.LastNameStr" ng-blur="findUser()" class="form-control" value="{{LASTNAME}}" />
        <input id="companyTb" ng-model="formData.COMPANYNAME" placeholder="@Model.CompanyStr" ng-blur="findUser()" class="form-control" value="{{COMPANYNAME}}" />
        <input id="codeTb" ng-model="formData.CHECKCODE" placeholder="@Model.CodeString" title="@Model.CodeStringDesc" ng-blur="findUser()" class="form-control" />
    </div>

    <div id="checkoutForm" class="form" ng-show="matchCount==1">
        <h3>@Html.Raw(@Model.RecordFoundStr)</h3>
    </div>
</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top