无法读取null的属性“join”

时间:2016-07-26 14:14:03

标签: javascript html angularjs

我是AngularJS的新手,我不知道我的代码有什么问题。试过很多变化,但每次都会得到错误的结果。

<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
    <title>acronyms</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Fredericka the Great">
</head>
<body>
    <div class="container-fluid col-sm-4">
<h1>Something</h1>
        <form role="form">
            <div class="form-group">
                <input name="userInput" id="userInput" class="form-control input-normal" placeholder="Enter Text Here" ng-model="userInput">
                <br>
            </div>
            <div>The acronym for "{{userInput}}" is <span main-directive id="result" ng-bind="acronymOutput"></span></div>
        </form>
    </div>
    <script>
        var myApp = angular.module("myApp", []);
        myApp.directive("mainDirective", function(){
            var inputValue = document.getElementById("userInput").value;
            var matches = inputValue.match(/\b(\w)/g);
            var acronymOutput = matches.join("").toUpperCase();
            document.getElementById("result").innerHTML = acronymOutput;
            document.getElementById("userInput").value = ("");
            return {
                template: ("<h1>" + acronymOutput + "</h1>")
            };
        })

5 个答案:

答案 0 :(得分:2)

将输入响应绑定到模型,将该模型传递给您的指令并从那里处理

答案 1 :(得分:0)

请在与reg匹配后查看var matches。 EXPR。

var matches = inputValue.match(/\b(\w)/g);
console.log(matches); // check in console
var mnemonicOutput="";
if(typeof matches == "array")
{
   mnemonicOutput = matches.join("").toUpperCase();
}

答案 2 :(得分:0)

你可以这样写。

Array.isArray(matches) && matches.join("").toUpperCase();

OR

Object.prototype.toString.call( someVar ) === '[object Array]' && matches.join("").toUpperCase();

答案 3 :(得分:0)

您是否尝试使用正则表达式来创建首字母缩略词? 如果是这样,我认为最好用“”拆分然后加入。 替换这两行:

var matches = inputValue.match(/\b(\w)/g);
var mnemonicOutput = matches.join("").toUpperCase();

有了这个:

var matches = inputValue. split(' ').map(function(item){return item[0]}).join('');
var mnemonicOutput = matches.toUpperCase();

答案 4 :(得分:0)

您首先犯的错误是在需要触发控制器功能的输入值时使用directive。 只需传递ng-model中的值,处理它并将其恢复。

    var myApp = angular.module("myApp", []);
    myApp.controller("AcronymController", function($scope) {
      var vm = this;

      vm.getAcronym = function() {
        var matches = vm.userInput.match(/\b(\w)/g);
        vm.acronymOutput = matches.join("").toUpperCase();
        document.getElementById("userInput").value = ("");
      };
    });

这是一个有效的jsfiddle: https://jsfiddle.net/9zL5nj8e/1/