0

当我尝试使用 phonegap 在 AngularJS 中打开模式时,我遇到了这个问题。

我正在检查来自设备的连接,如果设备离线,我需要打开一个模式。

我的 index.html 页面上有这段代码:

<script type="text/javascript" charset="utf-8" >

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        var $injector = angular.bootstrap(document, ['sms']);
        var $controller = $injector.get('$controller');
        var scope = $injector.get('$rootScope').$new();
        var DeviceCtrl = $controller("DeviceCtrl", {$scope: scope});
        scope.checkDevice(device.uuid, navigator.network.connection.type);
    }

</script>
...

 <div ng-controller="ModalCtrl" ng-include="'app/partials/modals.html'">

该脚本调用checkDevice函数在DeviceCtrl. 在这一点上确定。

DeviceCtrl:

function DeviceCtrl($scope, $window, Device, $rootScope) {

$scope.checkDevice = function (deviceUuid, networkState) {

    if (networkState == 'none') {
        $rootScope.$broadcast('openConectionless');
    }  
}

还有我的 ModalCtrl:

function ModalCtrl($scope) {
$scope.$on('openConectionless', function() {
    angular.element("#conectionModal").modal("show");

});

}

openConectionless侦听器被调用,但不angular.element("#conectionModal").modal("show");影响模式。

注意:如果我将conectionModaldiv 放在 index.html 上并调用angular.element("#conectionModal").modal("show");DeviceCtrl不是$rootScope.$broadcast('openConectionless');,则模式打开。但我想将模态代码移动到另一个 html 以使代码干净。所以我创建了 ModalCtrl。

有人可以帮助我吗?

4

1 回答 1

1

I'm guessing you are using bootstrap as modal plugin and modal is auto initialized on page load when the html is within index.html based on bootstrap data attribute in markup.

Since ng-include requires an AJAX request, the modal html doesn't exist when modal plugin runs initially so your modall is never intialized.

You could create a simple directive that will run the initialization code and add an attribute to the modal markup for that directive

于 2013-03-30T15:47:23.980 回答