简体   繁体   English

删除angular2中的数据绑定

[英]Remove data binding in angular2

Angular 2 data binding is great but i can't seem to find a angular 2 way of removing data binding on specific variables. Angular 2 数据绑定很棒,但我似乎找不到删除特定变量数据绑定的 Angular 2 方法。 My reason for this is i started hooking my application up to indexed DB and it works but i can't allow the temporary cache (just an array of all the indexed DB values) to be subject to data binding (if it was then the temporary cache would no longer mirror the database) my database is on an angular2 service.我这样做的原因是我开始将我的应用程序连接到索引数据库并且它可以工作,但我不能允许临时缓存(只是所有索引数据库值的数组)受制于数据绑定(如果是临时缓存)缓存将不再镜像数据库)我的数据库位于 angular2 服务上。 now i have found a way of removing the data binding but it isn't exactly pretty my code is this现在我找到了一种删除数据绑定的方法,但它并不是很漂亮我的代码是这样的

app.copy=function(item){
    return JSON.parse(JSON.stringify(item,app.replacer),app.reviver);
}
app.reviver=function(key,value){
    if(value.fn){
        value=new Function(value.parameters,value.body);
    }else if(key==="time"){
        value= new Date(value);
    }
    return value;
};
app.replacer=function(key,value){
    if(typeof value ==="function"){
        value=value.toString(); 
        value={
            fn:true,
            parameters:value.match(/\(([\s\S]*?)\)/)[1].replace(/[\s\r\/\*]/g,""),
            body:value.match(/\{([\s\S]*)\}/)[1].replace(/[\t\r\n]/g,"")
        };
    }
    return value;
};

like i said it works but it isn't pretty.就像我说的那样有效,但并不漂亮。 i can just run app.copy on the variables before they leave the cache so that they don't get data bound to anything.我可以在变量离开缓存之前在变量上运行 app.copy,这样它们就不会将数据绑定到任何东西。 I was wondering if there was a cleaner way to tell angular 2 this variable isn't suppose to be data bound.我想知道是否有更简洁的方法来告诉 angular 2 这个变量不应该是数据绑定的。 and if not then at least i was able to get my solution up here for others.如果没有,那么至少我能够为其他人提供我的解决方案。

If you establish "binding" imperatively you can stop the binding imperatively.如果您强制建立“绑定”,则可以强制停止绑定。 There is currently no support in Angular2 to cancel a declarative binding imperatively.目前 Angular2 不支持强制取消声明性绑定。

  • Bind the view only to fields of the component.仅将视图绑定到组件的字段。

  • Use observables in the service that fire an event when values change.在服务中使用可在值更改时触发事件的 observable。

  • In the component subscribe to the observable and update the fields in the component when values in the service change.在组件中订阅 observable 并在服务中的值更改时更新组件中的字段。

  • Update values in the service when values change in the component.当组件中的值更改时更新服务中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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