ng-show 对 $scope.apply 没有反应
ng-show does not react to $scope.apply
我有以下标记:
<div class="liveResults">
<div><img ng-show="home!==undefined" ng-src="{{homeImg}}"> </div>
<div>0:0</div>
<div><img ng-show="guest!==undefined" ng-src="{{guestImg}}"> </div>
</div>
在我的控制器中,我按以下方式填充数据:
$scope.init=function() {
$.getJSON("http://www.example.com/somedata.json", function(result){
$scope.home=result.home;
$scope.guest=result.guest;
$scope.homeImg=$scope.BigImage($scope.home);
$scope.guestImg=$scope.BigImage($scope.guest);
$scope.$apply();
return;
}
}
那个函数是 运行 by ng-init="init()"
现在 ng-src 已正确填充,但 ng-show 无法按预期工作(图像仍然隐藏)我可以使用浏览器 devtools 看到图像在那里,但具有宽度和高度的“0”。
当我通过将其分配给 ng-click-event 再次启动该功能时,图像显示正确。
我的猜测是 "ng-init" 和“$scope.apply”的组合导致了问题。
ng-src
指令中已经有一个隐含的 ng-show
,所以正如您问题中的评论所说明的那样,这应该已经对您有用了:
<div class="liveResults">
<div><img ng-src="{{homeImg}}"> </div>
<div>0:0</div>
<div><img ng-src="{{guestImg}}"> </div>
</div>
我有以下标记:
<div class="liveResults">
<div><img ng-show="home!==undefined" ng-src="{{homeImg}}"> </div>
<div>0:0</div>
<div><img ng-show="guest!==undefined" ng-src="{{guestImg}}"> </div>
</div>
在我的控制器中,我按以下方式填充数据:
$scope.init=function() {
$.getJSON("http://www.example.com/somedata.json", function(result){
$scope.home=result.home;
$scope.guest=result.guest;
$scope.homeImg=$scope.BigImage($scope.home);
$scope.guestImg=$scope.BigImage($scope.guest);
$scope.$apply();
return;
}
}
那个函数是 运行 by ng-init="init()"
现在 ng-src 已正确填充,但 ng-show 无法按预期工作(图像仍然隐藏)我可以使用浏览器 devtools 看到图像在那里,但具有宽度和高度的“0”。
当我通过将其分配给 ng-click-event 再次启动该功能时,图像显示正确。
我的猜测是 "ng-init" 和“$scope.apply”的组合导致了问题。
ng-src
指令中已经有一个隐含的 ng-show
,所以正如您问题中的评论所说明的那样,这应该已经对您有用了:
<div class="liveResults">
<div><img ng-src="{{homeImg}}"> </div>
<div>0:0</div>
<div><img ng-src="{{guestImg}}"> </div>
</div>