在ng-click异步后更新ng-class with function

时间:2015-04-07 15:13:59

标签: angularjs angularjs-factory

稍后使用函数更新ng-class ng-click asyncronous。我无法调用函数$ scope.addedProgram刷新视图。有什么建议吗?

在视图中,我有一个图标,根据事件是否已在日历上创建

而更新
// View
<ons-icon icon="ion-bookmark" size="30px" ng-click="program(paper)" ng-class="addedProgram(paper) ? 'active_bookmark' : 'inactive_bookmark'"></ons-icon>

控制器向工厂发出两次调用,用于ng-click和ng-class

 // Controller
    app.controller('AuthorController', function($scope, ProgramService) {

        // Program function()
        $scope.program = function(paper) {
            ProgramService.saveProgram(paper);
        }

        // Added Program function()
        $scope.addedProgram = function(paper) {
            return ProgramService.addedProgram(paper);
        }

    });

// Factory
app.factory('ProgramService', function($filter, $q) {
    var functions = {};

    functions.findProgramDevice = function(paper) {
        var deferred = $q.defer();

        var startDate = new Date(2015, 3, 7, 18, 30, 0, 0, 0);
        var endDate = new Date(2015, 3, 7, 19, 30, 0, 0, 0);
        var title = "My nice event";
        var eventLocation = "Home";
        var notes = "Some notes about this event.";
        var success = function(message) {
            if(message.length > 0){
                deferred.resolve(true); 
            }else{ 
                deferred.resolve(false); 
            } 
        };
        var error = function(message) { deferred.resolve(false); };

        window.plugins.calendar.findEvent(title, eventLocation, notes, startDate, endDate, success, error);

        return deferred.promise;
    }

    functions.saveProgramDevice = function(paper) {
        var startDate = new Date(2015, 3, 7, 18, 30, 0, 0, 0);
        var endDate = new Date(2015, 3, 7, 19, 30, 0, 0, 0);
        var title = "My nice event";
        var eventLocation = "Home";
        var notes = "Some notes about this event.";
        var success = function(message) { alert("Success 2: " + JSON.stringify(message)); };
        var error = function(message) { alert("Error 2: " + message); };

        functions.findProgramDevice(paper).then(function(result) {
            if(result){
                window.plugins.calendar.deleteEvent(title,eventLocation,notes,startDate,endDate,success,error);
            }else{
                window.plugins.calendar.createEvent(title,eventLocation,notes,startDate,endDate,success,error);
            }
        });
    }

    functions.saveProgram = function(paper) {
        var option = 0;
        functions.findProgramDevice(paper).then(function(result) {
            var program_exist_device = result;

            // Here other code

        });
    }

    functions.addedProgram = function(paper) {
        functions.findProgramDevice(paper).then(function(result) {
            var program_exist_device = result;
            return program_exist_device;
        });
    }
    return functions;
});

1 个答案:

答案 0 :(得分:0)

您必须在ng-init中触发该功能,并使用$scope变量更新类。

<ons-icon ng-init="addedProgram(paper)" icon="ion-bookmark" size="30px" ng-class="updateClass ? 'active_bookmark' : 'inactive_bookmark'">Heya</ons-icon>

在您的控制器中:

$scope.addedProgram = function(paper) {
     return $timeout(function() {
       $scope.updateClass = true;
     }, 5000);
     // on success of that asyn call update the $scope.updateClass variable.
   }

<强> DEMO