显示基于$ scope变量值的元素

时间:2014-06-02 21:23:04

标签: angularjs ng-show

我是AngularJS的新手,正在尝试弄清楚如何根据变量值显示HTML元素。

我试过这个:

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

和此:

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

到目前为止,这是我的代码:

<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>

在我的.js文件中:

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;
        });
    }
}

该函数按预期触发,我可以看到计数并验证$ scope.matchCount是否正确地使用记录计数进行更新,但它根本不会影响UI。

我错过了什么?

1 个答案:

答案 0 :(得分:7)

div ng-show="matchCount==1"位于scope的{​​{1}}之外。

移动VisitorLogController中的ng-show应该会有效。

scope