无法在angularJS上添加cordova插件

时间:2015-11-27 15:07:55

标签: angularjs gruntjs ngcordova

我有一个angularJS应用程序。我想使用ngCordova($ cordovaNetwork)的插件添加ngCordova模块。但我无法成功将ngCordova添加到我的项目中。 我的角项目由Yeoman创建:

$ mkdir myFolderProjectAngular; cd myFolderProjectAngular
$ yo angular

然后,我添加了ngCordova:

$ bower install --save ngCordova
bower ngCordova#~0.1.23-alpha   cached git://github.com/driftyco/ng-cordova.git#0.1.23-alpha
bower ngCordova#~0.1.23-alpha validate 0.1.23-alpha against git://github.com/driftyco/ng-cordova.git#~0.1.23-alpha
bower ngCordova#*               cached git://github.com/driftyco/ng-cordova.git#0.1.23-alpha
bower ngCordova#*             validate 0.1.23-alpha against git://github.com/driftyco/ng-cordova.git#*
bower ngCordova#~0.1.23-alpha  install ngCordova#0.1.23-alpha

我的bower.json会自动更新:

{
  "name": "test-network",
  "version": "0.0.0",
  "dependencies": {
    "angular": "^1.4.0",
    "bootstrap": "^3.2.0",
    "angular-route": "^1.4.0",
    "ngCordova": "~0.1.23-alpha"
  },
  "devDependencies": {
    "angular-mocks": "^1.4.0"
  },
  "appPath": "app",
  "moduleName": "testNetworkApp",
  "overrides": {
    "bootstrap": {
      "main": [
        "less/bootstrap.less",
        "dist/css/bootstrap.css",
        "dist/js/bootstrap.js"
      ]
    }
  }
}

我在index.html上手动添加两行:

<script src="/lib/ngCordova/dist/ng-cordova.js"></script>
<script src="/cordova.js"></script>

当我运行“grunt serve”进行测试时,我的index.html被编辑,前两行被取消并替换为:

 <script src="bower_components/ngCordova/dist/ng-cordova.js"></script>

当我尝试添加cordova插件时,出现错误:

  

错误:当前工作目录不是基于Cordova的项目。

我不明白这个问题。

这是我的文件夹项目

.
├── app/
├── bower_components
│   ├── angular/
│   ├── angular-mocks/
│   ├── angular-route/
│   ├── bootstrap/
│   ├── jquery/
│   └── ngCordova
│       ├── bower.json
│       ├── CHANGELOG.md
│       ├── dist/
│       │   ├── ng-cordova.js
│       │   ├── ng-cordova.min.js
│       │   ├── ng-cordova-mocks.js
│       │   └── ng-cordova-mocks.min.js
│       ├── LICENSE
│       ├── package.json
│       └── README.md
├── bower.json
├── Gruntfile.js
├── node_modules/
├── package.json
├── README.md
└── test/

编辑: 我通过更改文件夹解决了我的第一个问题 我首先使用cordova创建一个全局文件夹

cordova create myApp com.organisation.myapp MyApp

在此文件夹中,我创建了角度项目

yo angular MyApp 

之后,我可以添加ngCordova与bower并添加cordova插件。

我的问题,我不能使用插件的功能。在app.js上,我有:

angular.module('myAppApp', ['ngRoute', 'ngCordova' ]) ...

在main.js上,我有例如(使用Toast插件):

 angular.module('myAppApp')
  .controller('MainCtrl', function ($scope, $cordovaToast) {
    console.log(">"); 
    $cordovaToast.show('Here is a message', 'long', 'center')
    .then(function(success) {
      // success
    }, function (error) {
      // error
    });
   console.log("<")
});
  

main.js:12:4   错误:$ window.plugins未定义   .show @ http://localhost:9000/bower_components/ngCordova/dist/ng-cordova.js:6593:9   @ http://localhost:9000/scripts/controllers/main.js:13:4   调用@ http://localhost:9000/bower_components/angular/angular.js:4523:14   实例@ http://localhost:9000/bower_components/angular/angular.js:4531:27   $ ControllerProvider / $这gethttp://本地主机:9000 / bower_components /角度/ angular.js:9197:18   ngViewFillContentFactory /&LT;。链接@ http://localhost:9000/bower_components/angular-route/angular-route.js:977:26   invokeLinkFn @ http://localhost:9000/bower_components/angular/angular.js:8841:9   nodeLinkFn @ http://localhost:9000/bower_components/angular/angular.js:8335:1   compositeLinkFn @ http://localhost:9000/bower_components/angular/angular.js:7731:13   publicLinkFn @ http://localhost:9000/bower_components/angular/angular.js:7611:30   createBoundTranscludeFn / boundTranscludeFn @ http://localhost:9000/bower_components/angular/angular.js:7749:1   controllersBoundTransclude @ http://localhost:9000/bower_components/angular/angular.js:8362:18   更新@ http://localhost:9000/bower_components/angular-route/angular-route.js:935:25   $ RootScopeProvider / $这gethttp://本地主机:9000 / bower_components /角度/ angular.js:16374:15   commitRoute /&LT; @ http://localhost:9000/bower_components/angular-route/angular-route.js:619:15   processQueue @ http://localhost:9000/bower_components/angular/angular.js:14792:28   scheduleProcessQueue /&LT; @ http://localhost:9000/bower_components/angular/angular.js:14808:27   $ RootScopeProvider / $这gethttp://本地主机:9000 / bower_components /角度/ angular.js:16052:16   $ RootScopeProvider / $这gethttp://本地主机:9000 / bower_components /角度/ angular.js:15870:15   $ RootScopeProvider / $这gethttp://本地主机:9000 / bower_components /角度/ angular.js:16160:13   做@ http://localhost:9000/bower_components/angular/angular.js:10589:36   completeRequest @ http://localhost:9000/bower_components/angular/angular.js:10787:7   requestLoaded @ http://localhost:9000/bower_components/angular/angular.js:10728:1   

编辑: 我发现了自己的错误。某些功能在浏览器上不起作用。

2 个答案:

答案 0 :(得分:0)

您可能在gruntfile中有一些任务,即在index.html中注入脚本,以及导致其导入更改的原因。

答案 1 :(得分:0)

您的项目没有config.xml,它是cordova项目的基础。 请看这里: [Error: Current working directory is not a Cordova-based project.]

要启动cordova项目,您应该使用命令

cordova create PROJECTNAME

然后您可以将项目文件复制到www文件夹