I have this in a controller:
我在控制器中有这个:
$scope.address = new Address();
$scope.hasAddress = $scope.address.street !== undefined && $scope.address.street.length > 0;
And this markup:
这个标记:
<p>{{address.street !== undefined && address.street.length > 0}}</p>
<p>{{hasAddress}}</p>
And then this, indirectly through some includes:
然后,间接通过一些包括:
<input type="text" ... ng-model="address.street" required>
When the input first appears, the street is undefined, so the rendered html says:
当输入首次出现时,街道未定义,因此渲染的html说:
<p>false</p><p>false</p>
as expected. But when I enter characters into the input, the html is rendered as:
正如所料。但是当我在输入中输入字符时,html呈现为:
<p>true</p><p>false</p>
Why doesn't the hasAddress variable update? It's changed indirectly by the address object. Is this a case where I use $scope.$apply()? (and oh no, a new thing where I don't know what the heck I'm doing).
为什么hasAddress变量不更新?它由地址对象间接改变。这是我使用$ scope的情况。$ apply()? (哦,不,我不知道我在做什么的新事物)。
1 个解决方案
#1
2
That's because nothing in your JS is telling it to update.
那是因为你的JS中没有任何东西告诉它更新。
Add to your input: ng-change="updateVar()"
, then in your JavaScript:
添加到您的输入:ng-change =“updateVar()”,然后在您的JavaScript中:
$scope.updateVar = function() {
$scope.hasAddress = $scope.address.street !== undefined && $scope.address.street.length > 0;
}
#1
2
That's because nothing in your JS is telling it to update.
那是因为你的JS中没有任何东西告诉它更新。
Add to your input: ng-change="updateVar()"
, then in your JavaScript:
添加到您的输入:ng-change =“updateVar()”,然后在您的JavaScript中:
$scope.updateVar = function() {
$scope.hasAddress = $scope.address.street !== undefined && $scope.address.street.length > 0;
}