$ urlRouterProvider.otherwise在控制器的功能中无法识别$ cordovaSQLite.execute(db,query,[])

时间:2016-01-23 11:00:12

标签: angularjs cordova ionic-framework

我的app.js中有一个州:

.state('app.kamus', {
    url: '/kamus',
    views: {
        'menuContent': {
            templateUrl: 'templates/kamus.html',
            controller: 'Kamus1Ctrl'
        }
    }
})
...

和:

$urlRouterProvider.otherwise('/app/kamus');

控制器:

.controller('Kamus1Ctrl', function($scope, $cordovaSQLite, $ionicLoading, $stateParams, $timeout) {

$ionicLoading.show({
    content: 'Loading',
    animation: 'fade-in',
    showBackdrop: true,
    maxWidth: 200,
    showDelay: 0
});

$scope.selectAll = function() {
    $scope.datas = [];
    var query = "SELECT * FROM data";
    $cordovaSQLite.execute(db, query, []).then(function(res) {
        $scope.datas = [];
        if (res.rows.length > 0) {
            for (var i = 0; i < res.rows.length; i++) {

                $scope.datas.push(res.rows.item(i));
            }
        } else {
            console.log("No results found");
        }
    }).finally(function() {
        $ionicLoading.hide();
    });
}
$scope.selectAll();

})

我将$ urlRouterProvider.otherwise指向/ app / kamus,以便在首次启动我的应用程序时显示kamus页面。但似乎$ urlRouterProvider.otherwise无法在Kamus1Ctrl上的selectAll()函数中执行$ cordovaSQLite.execute(db,query,[])。

但是当我使用其他页面作为我的$ urlRouterProvider.otherwise,即:$ urlRouterProvider.otherwise(&#39; app / other&#39;),然后我通过我的侧边菜单(按钮ui)转到/ app / kamus -sref =&#34; app.kamus&#34;),它只是有效。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这样做

<强>的index.html

<body>
    <div>  
    <h3>Index page</h3>
        <div ui-view="main"></div>
    </div>
</body>

<强> kamus.html

<div ui-view="view1"></div>

<强>控制器

var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('app');

    $stateProvider    

    .state('app', {
        abstract: true,
        url: '/app',
        views: {
            'main': {
                 templateUrl: 'kamus.html',
                 controller : 'Kamus1Ctrl'
            }
        }
    })
    .state('app.kamus', {
        url: '',
        views: {
            'view1@app': {
                template: "Im View1"
            }
        }
    });

});

routerApp.controller('Kamus1Ctrl', function($scope, $cordovaSQLite, $ionicLoading, $stateParams, $timeout) {
alert("Inside controller");
    $scope.selectAll = function() {
        alert("Inside selectAll");  
        //rest of the code
    }

    $scope.selectAll();
    })

注意:在ui-router中,点运算符用于内部状态。