Angular.js从指令返回值到控制器

时间:2014-06-30 14:57:59

标签: javascript angularjs angularjs-directive angularjs-scope

我的指令有问题。我找到了这个插件: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>

CONTROLLER

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>

CONTROLLER

    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 个答案:

答案 0 :(得分:18)

只需将hook / callback-method作为变量传递给指令:

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);
   }
});

编辑:已添加Working Plunker