简体   繁体   中英

Service to call and update angular controller $scope from outside of controller?

controller function:

$scope.init = function () {
            Words.get('init', $scope.randNum)
                .error(function (data) {
                    console.log('init error: ' + data.message);
                })
                .success(function (data) {
                    $scope.board = data.settings.board;
                    $scope.tray = data.tray;
                    $scope.scores = data.pointsPerLetter;
                    $scope.totalScore = data.score.total;
                    console.log('init: ' + $scope.tray);
                })
        }

and my service:

angular.module('wordService', [])
    .factory('Words', function ($http) {
        var id;
        return {
            get: function (call, num) {
                id = num;
                return $http.get('http://xxxxx');
            },
            send: function (call, data) {
                console.log('send: ' + data)
                return $http.post('http://xxxxx' + call, data);
            }
        }
    });

Now instead of ngAccess = angular.element(document.getElementById('ws')).scope(); to call ngAccess.init() or $scope.init

How would I add this call to a service and thus call it when needed while still updating the scope within the controller? The reason the above will not work is that I am using browserify and I dont yet have access to the scope.

Scenario: I need to be able to click a button and call a function that updates the scope. Caveat: the button is created and added to a canvas. (shouldnt matter as I still have the click calls etc).

As always thanks in advance!

Move the data object into the service and assign a reference to a controller scope variable...

Your factory might look like:

.factory('Words', function ($http) {
    var id;
    var results = {};

    var get = function (call, num) {
        id = num;
        return $http.get('http://xxxxx').then(function(response){
            results.board = response.data.settings.board;
            results.tray = response.data.tray;
            results.scores = response.data.pointsPerLetter;
            results.totalScore = response.data.score.total;
        };
    };

    var send = function (call, data) {
        console.log('send: ' + data)
        return $http.post('http://xxxxx' + call, data);
    };

    return {
        get: get,
        send: send,
        results: results
    }
});

While your controller would then look like:

.controller(function($scope, Words){
    $scope.words = Words.results;
    $scope.init = function () {
        Words.get('init', $scope.randNum).then(function(){
            console.log($scope.words); // should log the data you want
        }, function(response){ console.log(response)});
    };
    // still calling from controller but you could from any component and still
    // have the local scope variable update based on its assignment to the
    // service object
    $scope.init(); 
})

Note that I did modify your factory a bit more to use the revealing module pattern. This way, you can make internal calls to your get/set functions in addition to calls from other components.

Now you should be able to add a button virtually anywhere else in the app (ie doesn't need prototypical inheritance from your controller's scope). For example, this directive would make a call and update the results, which would reflect in the controller scope variable

.directive('update', function(Words){
    return function(scope) {
        scope.update = function(){
            Words.get('update', 'somevalue')
        }
    }
})

where it is declared in the view like this:

<button update ng-click="update()">Update</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM