javascript - 类型错误 : Cannot read property 'notification' of undefined

标签 javascript angularjs ionic-framework

尝试实现本地通知,结果出现以下错误:

类型错误:无法读取未定义的属性“通知”

有问题的代码是,

function(){ 
    $ionicPlatform.ready(function() {
        $cordovaLocalNotification.add({
            id: '1',
            message: "Push!!"
        })
    }, false);  
    return true
    }

我从 ionic tabs 示例制作了应用程序。发生此调用的 Controller 看起来像

已更新

angular.module('starter.controllers', ['ionic','ngCordova']) .controller('FriendsCtrl', function($scope, $ionicPlatform, $cordovaLocalNotification,Friends) {

index.html 看起来像

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/controllers.js"></script>
<script src="js/services.js"></script>
<script src="js/filters.js"></script>
<script src="cordova.js"></script>

更新 $ionicPlatform is not defined 已修复,真正的问题在于 TypeError:无法读取未定义的属性“通知”

最佳答案

You're facing this error because, $ionicPlatform is not available in the global scope of your application.

似乎您正在尝试在应用程序加载的第一个实例上运行某些东西。好吧,为什么不用 Angular 方式呢?/

    angular.module("starter", ['ionic']).
    run(function($rootScope, $location, $ionicPlatform, $state) {
        $ionicPlatform.ready(function() {
            $cordovaLocalNotification.add({
                id: '1',
                message: "Push!!"
            })
        }, false);  
    });

From the Docs :

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the service have been configured and the injector has been created.

关于javascript - 类型错误 : Cannot read property 'notification' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26856876/

相关文章:

javascript - 文档就绪,$(document).on ("xxx") 和函数 onDeviceReady()

angularjs - 未使用 Angular $http 设置 Content-Type header

html - 个人资料照片上的按钮

javascript - 如何允许单击每个项目/帖子,然后在新屏幕中查看它?

javascript - Azure 应用服务 EasyApi - 如何访问表数据(CRUD)

Javascript 函数数组删除项目

javascript - 如何在单击时调用不同 div 中的 div 中的数据?

javascript - 如何使用 AngularJS 通过已检查的值检查复选框?

javascript - 如何在 angularjs 中重定向页面

javascript - D3 Canvas 图形在 Webview 中缩放和平移缓慢重绘