未定义未参考的referenceerror - 在使用Angular时添加javascript函数

时间:2015-07-24 22:25:55

标签: javascript jquery html angularjs

我熟悉Javascript和Jquery,但对于角度来说却相当新。我过去曾使用jquery和angular,但是我继承的这个应用程序引起了我的严重悲痛。

我点击按钮获取:

Uncaught ReferenceError: addComment is not defined

代码:

    <div ng-controller="TipsCtrl" class="container" id="tips">

    <div class="grid" style="margin-bottom: 0" >
    <div class="col-1-4">
    </div>
    <div style="text-align: center" class="col-1-2">
      <label class="text-blue text-center">Sort By:</label>
      <select style="width:30%" ng-init="order='created'" class="input" ng-model="order">
        <option value="created">Date Created</option>
        <option value="createdBy">Created By</option>
        <option value="getCategory()">Category</option>
      </select>
      <button data-tooltip="Reverse the sorting order."
        class="btn-blue" ng-click="reverse=!reverse">Reverse</button>
    </div>
    <div class="col-1-4 text-right">
      <button ng-click="createEmptyTip()" style="vertical-align: bottom" class="btn-blue">Add</button>
    </div>
    </div>
    <div class="col-1-1" ng-if="!!error">
    <div class="panel">
      <span class="text-red">Error:</span> {{ error }}
    </div>
     </div>
       <div update="resultsUpdated" paginate="page in tips" order="order" reverse="reverse" url="true">
     <div style="margin-bottom: 20px">
     <div paginate-controller></div>
    </div>
    <div ng-repeat="tip in page" class="tip"
    id="tip-{{ tip.id }}" async-anchor async-anchor-delay="500">
       <div class="grid">
         <div class="col-1-2">
        <h3>
           <span class="text-red" ng-if="tip.isNew()"><b>New:</b></span>
          {{ tip.title }}
          <div>Category: {{ tip.getCategory() }}</div>
        </h3>
      </div>
      <div class="col-1-2 text-right">
        <span>Posted by</span>
        <span class="text-blue">{{ tip.createdBy }}</span>
        <span>on {{ tip.created | date:'shortDate' }}</span>
        <span>{{ tip.created | date:'H:mm' }}</span>
      </div>
      <div class="col-1-1">
        <pre>{{ tip.body }}</pre>
      </div>
      <div class="col-3-4">
        <ul ng-if="tip.attachments.length > 0" class="attachment-list">
          <li ng-repeat="attachment in tip.attachments">
            <a href="{{ attachment.getDownloadLink() }}">{{ attachment.name }}</a>
          </li>
        </ul>
        <div class="col-1-1">
        </div>
        <div class="col-1-1">
            <a href="#/tips/comments/{{ tip.id }}?paginatePage={{ params.paginatePage }}" class="btn-yellow">Angular Add Comment</a>
            <button onclick="addComment();" id="addcomment/{{tip.id}}" class="btn-yellow">Add Comment</button>
        </div>
        </div>
        <!---->
      <div class="col-1-4 text-right" ng-if="tip.createdBy == user.ntid">
        <a href="#/tips/edit/{{ tip.id }}?paginatePage={{ params.paginatePage }}" class="btn-yellow" >Edit</a>
        <!--<a href="#/tips/delete/{{ tip.id }}" class="btn-red">Delete</a>0-->
        <button ng-ask="deleteTip(tip)" title="Delete"></button>
      </div>
    </div>
  </div>
   <div style="margin-top: 20px">
    <div paginate-controller></div>
   </div>
   </div>
   </div>

我在代码中调用addComment()函数(我先尝试过jquery)

<script>
    function addComment() {
        console.log('in');
    }
</script>

1 个答案:

答案 0 :(得分:1)

尝试将该功能移出标记并进入控制器......

...inside TipsCtrl...
$scope.addComment = function() {
    console.log('in');
};