網站搜尋

javascript - 是否有可能在 ng-class 裡面宣告註解


我有一個問題,是否可以在 ng-class 裡面宣告註解

ng-class="{'test':true,'test1':true, //some comment 'test2':true}"

答案#1

看起來並沒有。你需要像這樣使用多行註解語法:

<div ng-class="{'test':true,'test1':true, /* some comment */ 'test2':true}"></div>

但那會拋出錯誤:

語法錯誤:在表達式 [{'test':true,'test1':true, /* some comment */ 'test2':true}] 的第 29 欄遇到意外的符號 '*',預期為 [:],錯誤從 [* some comment */ 'test2':true}] 開始。

不過你可以透過在控制器/指令中宣告你的樣式來解決這個問題:

$scope.styles = {
    'test': true,
    'test1': true,
    /* some comment */ 'test2': true
};

並將其納入你的觀點中:

<div ng-app="MyApp">
    <div ng-controller="MyCtrl">
        <div ng-class="styles"></div>
    </div>
</div>

jsFiddle 範例

答案#2

不行,無法在 JSON 或 HTML 的 ng- 表達式中添加註解,因此如果你需要註解,請在標準 HTML 中的某個位置使用(不要放在標籤的參數之間,否則會“崩潰”)。可以放在標籤之前或之後。但要記住,註解對查看你頁面原始碼的用戶是可見的。在發布之前,使用一些壓縮工具刪除生產環境中的這些註解。