Angular.js指令如何将返回值传递给控制器

10

我有一个指令问题。我找到了这个插件:http://allensarkisyan.github.io/VideoFrame/,现在我已经在我的控制器中实现了这个插件:

HTML

<div class="row" ng-controller="searchDetailCtrl">
<section id="videoContainer">
    <video id="videoPlayer" width="100%" controls src="asset/tc3.mp4" type="video/mp4">
    </video>
</section>

<section id="videoControls">
    <div class="row more-margin-bottom" ng-repeat="asset in AssetDetail">
        <div class="col-sm-12">
            <button ng-click="playVideo()" class="btn btn-large btn-inverse"><i class="fa fa-play-circle fa-2x"></i></button>
                <button ng-click="pauseVideo()" class="btn btn-large btn-inverse"><i class="fa fa-pause fa-2x"></i></button>
                <button ng-click="seekBackward()" class="btn btn-large btn-inverse"><i class="fa fa-step-backward fa-2x"></i></button>
                <button ng-click="seekForward()" class="btn btn-large btn-inverse"><i class="fa fa-step-forward fa-2x"></i></button>
                <button id="markIn" ng-click="markIn()">MARKIN</button>
                <button id="markOut" ng-click="markOut()">MARKOUT</button>
                <button id="savePicture" ng-click="createPreviewScreenShot()">PICTURE</button>
            </div>
        </div>
    </section>
</div>

控制器

mwm3.controller('newSegmentationCtrl', function($scope, $timeout, SegmentationService, $route) 
{
    var jsonTimecodeArr = [];
    var jsonPictureArr = [];
    var pictureObj = new Object();
    var LogicalMedia = new Object();
    var LogicalMediaDetail = new Object();
    var Segment = new Object();
    var sequenceNumber = 0;
    var markInTime, markOutTime;
    var PictureJson;
    var timecodeJson;
    LogicalMediaDetail.Segment = [];
    $('#markIn').addClass('animated').addClass('bounce');
    $('#markOut').prop('disabled', true);
    $('#savePicture').prop('disabled', true);
    SegmentationService.connect();
    SegmentationService.subscribe(function(message) 
    {
        var obj;
        try 
        {
            //$scope.ocw.push(message);
            obj = eval("(function(){return " + message + ";})()");
            console.log(message);

            // $location.url('/searchDetail');
        } catch (e) {
            obj = eval("(function(){return " + message + ";})()");
            alert(obj.Error);
        }
    });
    var video = new VideoFrame({
    id: 'videoPlayerSegmentation',
    frameRate: 25,
    callback: function(response) {
        console.log(response);
    }
});

$scope.playVideo = function() {
    video.video.play();
};

$scope.pauseVideo = function() {
    video.video.pause();
};
$scope.seekForward = function() {
    video.seekForward('', video.triggerFrameUpdate);
};

$scope.seekBackward = function() {
    video.seekBackward('', video.triggerFrameUpdate);
};

$scope.markIn = function() {
    markInTime = video.toSMPTE();
    var typeCut = 'MARKIN';
    jsonTimecodeArr.push('{ "frame": "' + markInTime + '" , "typeCut": "' + typeCut + '" }');
    timecodeJson = buildJSON();
    $scope.timecodeList = timecodeJson;
    $scope.$apply;

    $('#markIn').prop('disabled', true);
    $('#markOut').prop('disabled', false);
    $('#markIn').removeClass('animated').removeClass('bounce');
    $('#markOut').addClass('animated').addClass('bounce');
    $.notify("MARKIN ESEGUITO", "success");
};

$scope.markOut = function() {
    markOutTime = video.toSMPTE();
    var s = verifyTimecode(markOutTime, markInTime);
    var the_char = s.charAt(0);

    if (the_char != '-') {
        var typeCut = 'MARKOUT';
        jsonTimecodeArr.push('{ "frame": "' + markOutTime + '" , "typeCut": "' + typeCut + '" }');
        timecodeJson = buildJSON();
        $scope.timecodeList = timecodeJson;
        $scope.$apply;

        $('#markOut').prop('disabled', true);
        $('#savePicture').prop('disabled', false);
        $('#markOut').removeClass('animated').removeClass('bounce');
        $('#savePicture').addClass('animated').addClass('bounce');
        $.notify("MARKOUT ESEGUITO", "success");
        sequenceNumber++;
    } else {
        $.notify("ATTENZIONE IL TIMECODE MARKOUT E' INFERIRE AL MARKIN", "error");
    }
};


$scope.createPreviewScreenShot = function() {
    var c = video.toSMPTE(),
    a = document.createElement("canvas");
    a.width = video.video.videoWidth;
    a.height = video.video.videoHeight;
    a.getContext("2d").drawImage(video.video, 0, 0);
    a = a.toDataURL("image/jpeg");
    pictureObj.Image = a;
    pictureObj.Timecode = c;
    jsonPictureArr.push('{ "Image": "' + a + '" , "Timecode": "' + c + '" }');
    PictureJson = buildJSONPicture();
    $scope.imagesList = PictureJson;
    $scope.$apply;
    console.log(PictureJson);
    $('#savePicture').prop('disabled', true);
    $('#savePicture').removeClass('animated').removeClass('bounce');
    $('#saveSingleSegment').removeClass('noDisplay').addClass('animated').addClass('bounce');

};

$scope.deleteSegment = function() {
    $('#markIn').prop('disabled', false);
    $('#markOut').prop('disabled', false);
    $('#saveSingleSegment').removeClass('fadeInDown').addClass('noDisplay');
    $.notify("ELIMINAZIONE SEGMENTAZIONE ESEGUITA CON SUCCESSO", "success");
    markInTime = '';
    markOutTime = '';
};

$scope.buildSingleSegment = function() {
    if ($scope.segmentName) {
        Segment.Sequence = sequenceNumber;
        Segment.Name = $scope.segmentName;
        Segment.SegmentImage = pictureObj.Image;
        Segment.TimeCodeIn = markInTime;
        Segment.TimeCodeOut = markOutTime;
        LogicalMediaDetail.Segment.push({
            Sequence: Segment.Sequence,
            Name: Segment.Name,
            SegmentImage: Segment.SegmentImage,
            TimeCodeIn: Segment.TimeCodeIn,
            TimeCodeOut: Segment.TimeCodeOut
        });
        $('#markIn').prop('disabled', false);
        $('#markOut').prop('disabled', false);
        $('#savePicture').prop('disabled', false);
        $('#markIn').addClass('animated').addClass('bounce');
        $('#saveSingleSegment').removeClass('animated').removeClass('bounce').addClass('noDisplay');
        $.notify("SEGMENTAZIONE SALVATO CON SUCCESSO", "success");
    } else {
        $.notify("INSERIRE IL NOME DELLA SEGMENTAZIONE", "info");
    }
}

$scope.sendSegment = function() {
    LogicalMediaDetail.Id = 0;
    LogicalMediaDetail.AssetId = idAssetSegmentation;
    LogicalMediaDetail.Name = $scope.segmentationName;
    LogicalMedia.LogicalMedia = LogicalMediaDetail;
    var myString = JSON.stringify(LogicalMedia);
    SegmentationService.send(myString);
    console.log(myString);
    $.notify("SEGMENTAZIONE SALVATA CON SUCCESSO", "success");
    $route.reload();
};

$scope.remove = function(image) {
    var index = $scope.imagesList.indexOf(image);
    var index2 = jsonPictureArr.indexOf(image);
    if (index != -1) {
        $scope.imagesList.splice(index, 1);
    }
    if (index2 != -1) {
        jsonPictureArr.splice(index, 1);
    }
};

function verifyTimecode(markOut, markIn) {
    var ms = moment(markOut, "HH:mm:ss:ms").diff(moment(markIn, "HH:mm:ss:ms"));
    var d = moment.duration(ms);
    var s = Math.floor(d.asHours()) + moment.utc(ms).format("HH:mm:ss:ms");

    return s;
}

function buildJSON() {
    var b = "[" + jsonTimecodeArr.join(",") + "]";
    return JSON.parse(b)
}

function buildJSONPicture() {
    var b = "[" + jsonPictureArr.join(",") + "]";
    return JSON.parse(b)
}

function getScreenShot() {
    var c = video.toSMPTE(),
    a = document.createElement("canvas");
    a.width = video.video.videoWidth;
    a.height = video.video.videoHeight;
    a.getContext("2d").drawImage(video.video, 0, 0);
    a = a.toDataURL("image/jpeg");
}

我想将这个指令转换为可在其他页面重用的指令,但我不明白如何从指令中返回现在在我的控制器$scope函数中的值。

例如,在我的控制器中有:

});

$scope.markIn = function() {
    markInTime = video.toSMPTE();
    .....
};

我希望从指令中将markInTime返回给我的控制器。

你能帮我理解吗?

提前致谢。

附加信息

HTML

 <div ng-controller="TestPlayerCtrl">
    <div my-directive my-directive-fn="controllerFunction"></div>
</div>

控制器

    mwm3.controller("TestPlayerCtrl", function($scope){
  $scope.controllerFunction = function(valueFromDirective){
     console.log(valueFromDirective);
   }
});

指令

    mwm3.directive("myDirective", function(){
  return {
   scope: {
     "myDirectiveFn": "="
   },
   link: function($scope){
     $scope.somethingHappend = function(){
       $scope.myDirectiveFn("pass this string");
     }
   }
  }
});

但是我在控制台中看不到任何输出。提前感谢。

1个回答

19

只需将钩子/回调方法作为变量传递到指令中即可:

app.directive("myDirective", function(){
  return {
   scope: {
     "myDirectiveFn": "="
   },
   link: function($scope){
     $scope.somethingHappend = function(){
       $scope.myDirectiveFn("pass this string");
     }
   }
  }
});

同时在HTML中

<div my-directive my-directive-fn="controllerFunction"></div>

并且控制器

app.controller("someCtrl", function($scope){
  $scope.controllerFunction = function(valueFromDirective){
     console.log(valueFromDirective);
   }
});

编辑:添加了可运行的Plunker


嗨,Konstantin Krass,我尝试了ID但对我不起作用。您可以在下一个答案中查看我的测试。 - user3790694
当然,你需要触发指令 somethingHappened 函数。请查看我的工作示例链接。 - Konstantin Krass

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接