Search code examples
angularjswin-universal-appwindows-10winjs

How do I do WinJS events the angular way?


There's a nice project for using Angular with WinJS controls together here: https://github.com/winjs/angular-winjs. I already have this working nicely in my app. However it doesn't really cover the use of angular for other parts of WinJS.

I am devloping on a windows 10 phone, and I have code like this:

 var app = WinJS.Application; 
 var activation = Windows.ApplicationModel.Activation; 
 app.onactivated = function (args) { 

How can I modify my app.onactivated assigment to do things the angular way so I can use $providers and modify $scope variables and so on?


Solution

  • By $providers do you mean the services that angular offers(like $http,$location and so on)?

    If so, you can put onactivated method inside the controller callback Definition function(see below codes) so that it can use the angular services.

    JS:

    (function () {
    "use strict";
    
    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var myApp = angular.module("myApp", ["winjs"]);
    var myController = myApp.controller("myController", function ($scope) {
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                    $scope.$apply(function () {
                        $scope.test = "The Scope.test has been changed!";
                    });
                }
            args.setPromise(WinJS.UI.processAll());
            }
        };
    });
    app.start();
    })(angular);
    

    HTML:

    <body class="win-type-body" ng-app="myApp">
    <div ng-controller="myController">
    <input type="text" ng-model="test"/>
       {{test}}
    </div>
    

    If not so, can you tell me what $providers are?