简体   繁体   English

Ionic v1 / Angular - 从 DOM 中删除以前的离子视图

[英]Ionic v1 / Angular - Remove Previous ion-view from DOM

I am modifying an App written in Ionic v1.我正在修改一个用 Ionic v1 编写的应用程序。

There is a main View called 'Client's Profile' which has links to two other Views: Support Request and Complaints.有一个称为“客户资料”的主视图,其中包含指向其他两个视图的链接:支持请求和投诉。

Inside the Support Request views there is a TextArea with Id " description ", there is also a function that it's called when a button is tapped which gets the content of this textarea with jQuery.在 Support Request 视图中,有一个 ID 为“ description ”的 TextArea,还有一个函数,当点击按钮时会调用该函数,该函数使用 jQuery 获取此 textarea 的内容。

var request_description = $("#description").val();

After the execution of this function, you come back to the Main View (Client's Profile).执行此功能后,您将返回主视图(客户资料)。

If then you open the 'Complaints' View, here there is also a textarea with id " description " and there is another function that does the same and get's the content of this textarea with the same code above using jQuery.如果然后您打开“投诉”视图,这里还有一个 ID 为“ description ”的文本区域,还有另一个函数执行相同的操作,并使用上面使用 jQuery 的相同代码获取此文本区域的内容。

The problem is that this code fails because the ion-view of the previously opened 'Support Request' is still present in the DOM -ie the first occurence of the element with id "description" is matched, which belongs to the previous view and not the currently visible-.问题是这段代码失败了,因为之前打开的'Support Request'的ion-view仍然存在于DOM中——即id为“description”的元素的第一次出现是匹配的,它属于之前的视图而不是当前可见-。 I understand that probably the problem is using a selector to get a value directly from the DOM and that this is against what you are supposed to do, but since this is old code, and I don't have the time to change it around too much...我知道问题可能是使用选择器直接从 DOM 获取值,这与您应该做的相反,但由于这是旧代码,我也没有时间对其进行更改很多...

1) My question is why is this happening, is this the normal behavior of Angular -to keep previous views in the DOM- or am I missing something? 1) 我的问题是为什么会发生这种情况,这是 Angular 的正常行为 - 在 DOM 中保留以前的视图 - 还是我遗漏了什么?

2) Appart from giving each text area a different id, what can I do to either delete the previous ion-view from the DOM or prevent this behavior entirely. 2) Appart 给每个文本区域一个不同的 id,我能做些什么来从 DOM 中删除以前的 ion-view 或完全阻止这种行为。

After some digging, I found that inside my app.js I had the following code that triggered the switch between views:经过一番挖掘,我发现在我的app.js 中,我有以下代码触发了视图之间的切换:

        .state('app.Complaints', {
            url: '/Complaints',
            views: {
                'menuContent': {
                    templateUrl: 'templates/Client/Complaints.html',
                    controller: 'ComplaintsCtrl'
                }
            }
        });
        .state('app.SupportRequest', {
            url: '/SupportRequest',
            views: {
                'menuContent': {
                    templateUrl: 'templates/Client/SupportRequest.html',
                    controller: 'SupportRequestCtrl'
                }
            }
        });

Here the controller is binded to an HTML template, and the Url of this view/Controller is declared.这里控制器绑定到一个 HTML 模板,并声明了这个视图/控制器的 Url。 In here, you can add a new prop to the object that you pass to this initialization.在这里,您可以向传递给此初始化的对象添加一个新道具。 This prop is called cache and it can be set to false in order to stop the view from staying in the DOM after you leave it.这个 prop 被称为cache ,它可以设置为 false 以阻止视图在离开后停留在 DOM 中。 So after modifying my app.js the old ion-view containers dissapeared from the DOM after I left said view.因此,在修改我的app.js 后,旧的 ion-view 容器在我离开所述视图后从 DOM 中消失了。

.state('app.Complaints', {
            cache: false,
            url: '/Complaints',
            views: {
                'menuContent': {
                    templateUrl: 'templates/Client/Complaints.html',
                    controller: 'ComplaintsCtrl'
                }
            }
        });
        .state('app.SupportRequest', {
            cache: false,
            url: '/SupportRequest',
            views: {
                'menuContent': {
                    templateUrl: 'templates/Client/SupportRequest.html',
                    controller: 'SupportRequestCtrl'
                }
            }
        });

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

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