草庐IT

javascript - Angular.js 从指令到 Controller 的返回值

coder 2024-05-15 原文

我对指令有疑问。我找到了这个插件:http://allensarkisyan.github.io/VideoFrame/ ,现在我的 Controller 中有这个插件实现:

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

});

我想将此 in 指令转换为在其他页面中重用它,但我不明白如何从指令返回现在在我的 Controller $scope 函数中的值。

例如,在我的 Controller 中我有:

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

我想将 markInTime 从指令返回到我的 Controller 。

你能帮我理解一下吗?

提前致谢

附加信息

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

但我在控制台中没有看到任何输出。提前致谢

最佳答案

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

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>

和 Controller

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

编辑: 添加了 Working Plunker

关于javascript - Angular.js 从指令到 Controller 的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493049/

有关javascript - Angular.js 从指令到 Controller 的返回值的更多相关文章

  1. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  2. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  3. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  4. ruby-on-rails - rails : How to make a form post to another controller action - 2

    我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak

  5. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  6. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  7. ruby - Ruby 中的隐式返回值是怎么回事? - 2

    所以我开始关注ruby​​,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出

  8. ruby-on-rails - ruby 日期方程不返回预期的真值 - 2

    为什么以下不同?Time.now.end_of_day==Time.now.end_of_day-0.days#falseTime.now.end_of_day.to_s==Time.now.end_of_day-0.days.to_s#true 最佳答案 因为纳秒数不同:ruby-1.9.2-p180:014>(Time.now.end_of_day-0.days).nsec=>999999000ruby-1.9.2-p180:015>Time.now.end_of_day.nsec=>999999998

  9. ruby - 从 String#split 返回的零长度字符串 - 2

    在Ruby1.9.3(可能还有更早的版本,不确定)中,我试图弄清楚为什么Ruby的String#split方法会给我某些结果。我得到的结果似乎与我的预期相反。这是一个例子:"abcabc".split("b")#=>["a","ca","c"]"abcabc".split("a")#=>["","bc","bc"]"abcabc".split("c")#=>["ab","ab"]在这里,第一个示例返回的正是我所期望的。但在第二个示例中,我很困惑为什么#split返回零长度字符串作为返回数组的第一个值。这是什么原因呢?这是我所期望的:"abcabc".split("a")#=>["bc"

  10. ruby - 为什么 Integer.respond_to?( :even? ) 返回 false? - 2

    我一直在研究RubyKoans,我发现about_open_classes.rbkoan很有趣。特别是他们修改Integer#even?方法的最后一个测试。我想尝试一下这个概念,所以我打开了Irb并尝试运行Integer.respond_to?(:even?),但令我惊讶的是我得到了错误。然后我尝试了Fixnum.respond_to?(:even?)并得到了错误。我还尝试了Integer.respond_to?(:respond_to?)并得到了true,当我执行2.even?时,我也得到了true。我不知道发生了什么。谁能告诉我缺少什么? 最佳答案

随机推荐