Javascript AngularJS中的黑客打字效果(在随机按键时显示特定文本)

Javascript AngularJS中的黑客打字效果(在随机按键时显示特定文本),javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试在AngularJS中实现类似于此站点的效果。到目前为止,我已经根据中的响应得出了一些结果,但是我没有使用计时器,而是根据ng keyup进行计数 现在,每个单独的ng keyup显示一个字符。但我希望它能同时显示三个 这是我尝试过的方法,但它似乎只揭示了每三个角色。有什么建议吗 HTML <div ng-controller="WriterCtrl" ng-keyup="ghostWriter()"> <pre>KeyUps: {{count}}<

我正在尝试在AngularJS中实现类似于此站点的效果。到目前为止,我已经根据中的响应得出了一些结果,但是我没有使用计时器,而是根据
ng keyup
进行计数

现在,每个单独的
ng keyup
显示一个字符。但我希望它能同时显示三个

这是我尝试过的方法,但它似乎只揭示了每三个角色。有什么建议吗

HTML

<div ng-controller="WriterCtrl" ng-keyup="ghostWriter()">
    <pre>KeyUps: {{count}}</pre>
    <p>{{typewritten}}</p>
</div>

键控:{{count}
{{打字}}

JS

//Content to be ghost written    
var content = "a whole load of content to be ghost written";

//Content already ghost written
$scope.typewritten = '';

//The ghost writer!
$scope.ghostWriter = function() {

  //If there is still content left to write
  if ($scope.typewritten.length < content.length) {

    //Write up to three characters from the remaining content
    $scope.typewritten += content.substr($scope.typewritten.length, 3);

  } else {

    window.alert('Writing time is over.');

  }

};
app.controller('WriterCtrl',['$scope',
职能($范围){
var content=‘玛丽·安!玛丽·安!声音说’;
$scope.count=0;
$scope.typewrited='';
$scope.ghostWriter=函数(){
如果($scope.count
我想这正是你想要的

//要重影写入的内容
var content=“要重写的全部内容”;
//内容已经写好了
$scope.typewrited='';
//鬼作家!
$scope.ghostWriter=函数(){
//如果还有内容要写
if($scope.typewrited.length
我发现这有点问题-如果
content.length
恰好是3的倍数以外的任何值,那么它会在末尾键入一次或两次“undefined”。我问过我的朋友,他建议使用
.substr()
方法来设置这样的最大长度。感谢您的更正,这样更好。我已经更新了答案和密码。我还把它缩短了一点。
//Content to be ghost written    
var content = "a whole load of content to be ghost written";

//Content already ghost written
$scope.typewritten = '';

//The ghost writer!
$scope.ghostWriter = function() {

  //If there is still content left to write
  if ($scope.typewritten.length < content.length) {

    //Write up to three characters from the remaining content
    $scope.typewritten += content.substr($scope.typewritten.length, 3);

  } else {

    window.alert('Writing time is over.');

  }

};