如何从PhoneGap的插件访问的角度控制器的价值?控制器、插件、角度、价值

2023-09-13 05:20:30 作者:你已经不是我的回忆了〃

我新的角度和卡住的东西,我希望是简单的。我需要阅读使用这个插件(程序版本插件)应用程序的版本。我知道我需要阅读在运行处理器的版本,但我无法弄清楚如何让它进入我的控制器,以便我能数据绑定到它。

I'm new to Angular and getting stuck on something I hope is simple. I need to read the app version using this plugin (app version plugin). I know I need to read the version in the run handler but I can't figure out how to get it into my controller so I can data-bind to it.

下面是我的code,到目前为止这读取正确的版本:

Here's my code so far which reads the version properly:

var appVersion = "0.0.0";

var example = angular.module('surj', ['ionic'])
    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            cordova.getAppVersion.getVersionNumber(function (version) {
                appVersion = version;
            });
        });
    })

    .controller('TestCtrl', function() {
        var controller = this;

        //this.version = appVersion;
        this.getVersion = function() {
            return appVersion;
        }
    })

我敢肯定,我只是失去了一些东西明显。有什么想法?

I'm sure I'm just missing something obvious. Any thoughts?

谢谢,杰森

推荐答案

好吧,尝试许多不同的方法后,这里就是我想出了。

Okay, after trying many different methods, here's what I came up with.

var example = angular.module('App', ['ionic'])
    .controller('AppCtrl', ['$scope', '$ionicPlatform', function($scope, $ionicPlatform) {
        var controller = this;

        // Properties
        controller.appVersion = "NA";

        // Methods
        controller.setVersion = function (version) {
            $scope.$apply(function() { controller.appVersion = version });  // Update the value and trigger the data-binding to refresh
        }

        // Init
        $ionicPlatform.ready(function () {
            cordova.getAppVersion.getVersionNumber(function (version) {
                controller.setVersion(version);
            });
        });
    }])

这其实很简单,你只需用$范围。$申请更新一次数据绑定的版本被更新。

It's actually quite simple, you just use $scope.$apply to update the databinding once the version gets updated.