簡體   English   中英

Angular.js模型遍歷

[英]Angular.js Model Traversal

有沒有像$ .each(scope.model)這樣的函數? 遍歷作用域內所有模型的功能?

我似乎在任何地方都找不到答案,並認為也許應該嘗試一下。

謝謝!

-簡


編輯:

因此,我一直在研究代碼,並找到了解決方法。

在這里擺弄

function ResetScope(scope){
    $(scope).each(function () {
        if(!(this instanceof Function)){           
            for (var key in this) {
                if(key.indexOf("$") !== -1 || key.indexOf("this") !== -1)
                    continue;
                else
                    if(key instanceof Function){
                        continue;
                    }else if(this[key].indexOf("function") !== -1){                        
                        continue;
                    }else{
                        alert(this[key]);
                        this[key] = "";
                        console.log(this);
                    }
            }
        }
    });  
    return scope;
}

唯一不那么棒的事情是,當您在變量中使用名稱中帶有“函數”的變量時,該變量也可能會被過濾掉。 好吧,至少到目前為止,此代碼片段有效。 對於那些有答案的人,請隨時發布您的答案。 可能對別人有幫助。

作為參考,Brian的Batarang工具在appInspect.js中包含以下代碼:

var thisScope = angular.element(this).scope();
var models = {};
for (prop in thisScope) {
    if (thisScope.hasOwnProperty(prop) && prop !== 'this' && prop[0] !== '$') {
        models[prop] = thisScope[prop];
    }
}
var str = JSON.stringify(models);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM