无法在angularJS中读取未定义的属性'replace'?

时间:2014-08-19 09:02:45

标签: angularjs

我正在使用表情符号过滤器,我收到错误“无法读取属性'替换'未定义”

Errro是:

TypeError: Cannot read property 'replace' of undefined
        at g.<anonymous> (file:///C:/xampp/htdocs/web_UI/js/emoji.min.js:1:10531)
        at e (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:155:305)
        at Ia.| (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:143:248)
        at Object.A.constant [as get] (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:154:212)
        at g.$digest (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:98:307)
        at g.$apply (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:101:157)
        at HTMLAnchorElement.<anonymous> (file:///C:/xampp/htdocs/web_UI/js/angular.min.js:177:65)
        at HTMLAnchorElement.m.event.dispatch (file:///C:/xampp/htdocs/web_UI/js/jquery.min.js:3:8436)
        at HTMLAnchorElement.r.handle (file:///C:/xampp/htdocs/web_UI/js/jquery.min.js:3:5146)

Html代码:

<div class="row" ng-repeat="n in q.answers"  ng-bind-html = " n | emoji">

json回复:

{
    "data":[
            {
                "id":10,
                "title":"what is internet?",
                "userID":2,
                "question":"what is internet?",
                "viewed":0,
                "votes":5,
                "answers":[
                            {"id":15,
                            "questionID":10,
                            "userID":2,
                            "answer":"search on google ....:)",
                            "correct":"0",
                            "votes":7

                            },
                            {
                                "id":47,
                                "questionID":10,
                                "userID":2,
                                "answer":"test :smiley:",
                                "correct":"0","votes":0,
                           }
                    ]}
            ]
        }

js功能:

QAApp.controller('askbyctrl', function ($scope, $http){
            $http.get(server + 'api').success(function(data) {
            /*console.log(data);*/
            $scope.qa = data;
          });
          $scope.select = function(item) {
            /*console.log(item);*/
            $scope.selected = item
          }
          $scope.isSelected = function(item) {
            return $scope.selected == item
          }
      });

然后在html中使用 ng-repeat =“q in qa.data”并在q.answers中显示答案 n

请告诉我如何解决?

  

Plunker link

2 个答案:

答案 0 :(得分:3)

这是工作代码......

            angular.module("app", ['emoji', 'ngSanitize']).controller("AppCtrl", function ($scope) {
            $scope.messages = [
                "Animals: :dog: :cat: :snake:",
                "People: :smile: :confused: :angry:",
                "Places: :house: :school: :hotel:"
            ];
        });


<body ng-app="app" ng-controller="AppCtrl">
    <ul>
        <li ng-repeat="message in messages" ng-bind-html="message | emoji"></li>
    </ul>
</body>

答案 1 :(得分:2)

您需要将表情符号过滤器应用于字符串,但是您的n in q.answers循环遍历对象列表(而不是字符串)。您需要将HTML更改为:

<div class="row" ng-repeat="n in q.answers"  ng-bind-html = "n.answer | emoji">

选中plunker