Вызов метода директивы из контроллера: AngularJS - PullRequest
0 голосов
/ 27 января 2019

Я создал директиву, подобную следующей

.directive("sampleInstructionOne",['$q' , '$rootScope',  'HigiKioskStorageService', 'HigiKioskUtilitiesService',function($q, $rootScope, HigiKioskStorageService,HigiKioskUtilitiesService) {
    //Weight instruction 1
    return {
        restrict : 'E',
        scope : false,
        templateUrl : 'components/weight/ecg-instruction-1.html',
        link : function(scope, element, attr){
            scope.weightInstruction = scope.weightInstruction || new Object();
            scope.weightInstruction.isHigi= HigiKioskUtilitiesService.isHigiGreen();

            scope.weightInstruction.bmcAnimationOne = function(){
                var q = $q.defer();
                $('.bmc_instruction_place').css('opacity', 1);
                $('#bmc_instruction_place_frames').delay(50)//we don't want to use a timeout, so we use a delay
                    .animate({'backgroundPosition':'left top'}, 1, function () { //a dummy function to "restart" the animation at first frame AND have a callback where we set the sprite
                        $('#bmc_instruction_place_frames').sprite({ //sets the sprite and animates it immediately
                            fps:(scope.weightInstruction.isHigi) ? 24 : 18,
                            no_of_frames:(scope.weightInstruction.isHigi) ? 24 : 18,
                            start_at_frame:0,
                            play_frames:(scope.weightInstruction.isHigi) ? 24 : 18
                        });
                    })
                    .delay(2000)//a delay to wait until the sprite animation is completed. this number needs to be equal to how long the sprite animates
                    .animate({'backgroundPosition':'right top'}, 1, function () { //a dummy function to house the callback, but also to make sure the animation is at the last frame
                        q.resolve();
                        $('#bmc_instruction_place_frames').destroy(); //you MUST destroy the sprite if you want it to play again
                    });

                return q;
            };
            scope[attr.promisename].resolve();

        }
    }
}])

Мне нужно вызвать метод директивы scope.weightInstruction.bmcAnimationOne "в контроллере. Я попытался вызвать, как показано ниже, и в итоге" Cannot "прочитать свойство bmcAnimationOne undefined ".

 $scope.instructionTwo = [
            $scope.weightInstruction.bmcAnimationOne
        ];

1 Ответ

0 голосов
/ 30 января 2019

Когда вы пытаетесь получить доступ к $ scope.weightInstruction.bmcAnimationOne в родительском контроллере, поскольку bmcAnimation является обещанием, вероятно, оно еще не закончено, поэтому дляточка зрения контроллера, она еще не определена.

Вам следует подождать, пока она загрузится, а затем присвоить результат:

$scope.instructionTwo = [];
$scope.weightInstruction.bmcAnimationOne()
    .then(function (result) {
        $scope.instructionTwo.push(result));
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...