天天看點

Angular頁面調試一個有用的小技巧 - normalizeDebugBindingName和normalizeDebugBindingValue - [object Object]

在開發模式下渲染出的 Angular

頁面包含了很多形如下圖ng-reflect-的html屬性,很多時候其值都為[object Object].

如果處于調試目的,需要在Chrome開發者工具裡觀察這些值的具體内容,可以采取本文介紹的一個小技巧:

Angular頁面調試一個有用的小技巧 - normalizeDebugBindingName和normalizeDebugBindingValue - [object Object]
在下列兩個函數裡設定斷點:

  • normalizeDebugBindingName
  • normalizeDebugBindingValue
function normalizeDebugBindingName(name) {
    // Attribute names with `$` (eg `x-y$`) are valid per spec, but unsupported by some browsers
    name = camelCaseToDashCase(name.replace(/[$@]/g, '_'));
    return `ng-reflect-${name}`;
}
const CAMEL_CASE_REGEXP = /([A-Z])/g;
function camelCaseToDashCase(input) {
    return input.replace(CAMEL_CASE_REGEXP, (...m) => '-' + m[1].toLowerCase());
}
function normalizeDebugBindingValue(value) {
    try {
        // Limit the size of the value as otherwise the DOM just gets polluted.
        return value != null ? value.toString().slice(0, 30) : value;
    }
    catch (e) {
        return '[ERROR] Exception while trying to serialize the value';
    }
}      
Angular頁面調試一個有用的小技巧 - normalizeDebugBindingName和normalizeDebugBindingValue - [object Object]

繼續閱讀