简体   繁体   中英

Angular.js Model Traversal

Is there a function that goes like $.each(scope.model)? A function that traverses all the models within a scope?

I can't seem to find an answer anywhere and thought maybe I should give SO a try.

Thanks!

-Jan


EDIT:

So I've been playing around the code and found a workaround on this.

Fiddle here.

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;
}

The only thing that is not so awesome is that when you have a variable with a "function" in its name, it might also be filtered out. Well, at least for now, this snippet works. For those who have their answers, feel free to post your answer. Might be helpful to others.

For reference, Brian's Batarang tool has this code in 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM