gpt4 book ai didi

javascript - AngularJs:无法切换两个元素的可见性

转载 作者:行者123 更新时间:2023-11-28 19:42:37 25 4
gpt4 key购买 nike

我正在使用这种方法:

app.js

(function(){

var app = angular.module('myApp', ['fsCordova']);

app.controller('MyController', function($scope, CordovaService) {

this.ready = false;

CordovaService.ready.then(function() {

console.log ("CordovaService.ready received");
this.ready = true;
});

});

})();

可以在控制台中看到CordovaService.ready收到的消息。所以代码可以正常工作。

在我的index.html中,我有这个(仅有用的部分)。

    <body ng-controller="MyController">
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening" ng-hide="ready">Connecting to Device</p>
<p class="event received" ng-show="ready">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
</body>

为了完整性,这是 .css 文件的相关部分

.event.listening {
background-color:#333333;
}

.event.received {
background-color:#4B946A;
}

.blink {
animation:fade 3000ms infinite;
}

我再说一遍:可以在控制台中看到CordovaService.ready收到的消息。所以js代码正在工作。

问题在于:即使 MyController.ready 设置为 true,“正在连接到设备”仍然可见。

如何调试?

最佳答案

您应该使用 $scope 在 Controller 和模板之间共享状态。

app.controller('MyController', ['$scope', 'CordovaService', function($scope, CordovaService) {

$scope.ready = false;
CordovaService.ready.then(function() {
console.log ("CordovaService.ready received");
$scope.ready = true;
});

}]);

关于javascript - AngularJs:无法切换两个元素的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800043/

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