javascript - Angular ng-show 不适用于 Controller

标签 javascript angularjs

我想在视频结束后显示一个 div。视频结束后会出现警报,但 div 仍然不可见。我做错了什么。 提前感谢您的帮助。

@section scripts
{
    <script type="text/javascript" src="~/App/Controllers/TestController.js"></script>
}
<div ng-controller="TestController as ctrl" class="md-content" ng-cloak>
    <h1>Test</h1>

    <video id="myVideo"  controls autoplay>
        <source src="~/Videos/Test1.mp4">
    </video>

    <div id="test" ng-show="ui.showTest">
        <h1>Test</h1>
        Added this as a test to see if your controller is linked to the view
        <p>{{ui.shouldSeeThis}}</p>
    </div>
</div>

这是 Controller

angular.module('MyApp', ['ngMaterial', 'ngMessages'])
.controller('TestController', function ($scope, $http, $filter, $mdDialog) {


var vid;

function initializePlayer() {

    vid = document.getElementById("myVideo");
    vid.addEventListener('ended', videoFinish, false);
}

window.onload = initializePlayer;

$scope.ui = {
    showTest: false,
    //I've added this to see if your controller is linked to the view
    shouldSeeThis: "Hello World"
};
function videoFinish() {
    $scope.ui.showTest = true;
    alert("VideoFinish");
}

});

最佳答案

看起来您需要强制摘要以反射(reflect) View 中的范围更改。发生这种情况是因为您正在使用 javascript 事件更改范围值。

在你的 Controller 中添加$timeout:

.controller('TestController', function ($scope, $http, $filter, $mdDialog, $timeout)

在你的函数中用它包装代码:

function videoFinish() {
    $timeout(function() {
      $scope.ui.showTest = true;
      alert("VideoFinish");
    });
}

关于javascript - Angular ng-show 不适用于 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40135456/

相关文章:

javascript - JavaScript 中的 Django 模板实现?

javascript - Phonegap 未在 Angularjs 指令内触发 GoogleMaps v3 domListener 函数

javascript - 为什么我收到错误 : $injector:unpr Unknown Provider?

javascript - 更改另一个属性时的 Angular 更新属性

javascript - 点击几次后 CSS 未加载

javascript - 流抽象类型和函数 - 无法访问属性

javascript - 构造函数继承自构造函数

javascript - 尽管两个调用都存在,但失败并显示 : success/error was not called,

javascript - 从 json 生成序列

javascript - 如何在嵌套的 ngRepeat 中的第二个 ngRepeat 上应用过滤器?