gpt4 book ai didi

angularjs - 根据 $scope 变量值显示元素

转载 作者:行者123 更新时间:2023-12-01 04:53:51 25 4
gpt4 key购买 nike

我是 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。

我错过了什么?

最佳答案

带有 ng-show="matchCount==1"div 超出了 VisitorLogController范围 .

scope 内移动 ng-show 应该可以让它工作。

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

关于angularjs - 根据 $scope 变量值显示元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com