当$ scope被销毁时删除事件监听器

时间:2015-05-11 14:10:17

标签: javascript angularjs

我实际上遇到了一个问题。

我有一个监听类似事件的指令:

$scope.$on('event',myFunc);

问题是当控制器被销毁时,$ scope会使事件监听器保持活动状态。

我应该输入什么代码

$scope.$on('$destroy',myDestroyFunc);

防止这种影响?

感谢您提前

2 个答案:

答案 0 :(得分:1)

你应该监听范围销毁事件并从销毁事件中取消注册。 $ on返回事件的注销函数。

<camel:onException>
        <camel:exception>java.lang.Exception</camel:exception>
        <camel:redeliveryPolicy backOffMultiplier="500" />
        <camel:log message="Default error handler was called"></camel:log>
    </camel:onException>
            <camel:route>
                <!--  Reading from REST url -->
                <camel:from uri="<my url>" />
                <!--  If else loop -->
                <camel:choice>
                    <camel:when>
                        <camel:xpath>Some path</camel:xpath>
                        <!--  Parsing OrderNumber and OrderVersion-->
                        <camel:log message="Recieved request ${headers.OrderNumber}-${headers.OrderVersion}.xml"/>
                        <camel:setHeader headerName="OrderNumber">
                            <xpath>Some path</xpath>
                        </camel:setHeader>
                        <camel:setHeader headerName="OrderVersion">
                            <camel:xpath>Some path</camel:xpath>
                        </camel:setHeader>
                        <!-- Request being put in file folder -->
                        <to
                            uri="file:data/inbox?fileName=${header.OrderNumber}-${header.OrderVersion}.xml"
                            pattern="InOut" />
                    </camel:when>
                    <!--  For all other requests put on  queue -->
                    <camel:otherwise>
                        <camel:log message="Request ${headers.OrderNumber}-${headers.OrderVersion}.xml directly sent to  queue"/>
                            <to uri="my queue"
                                pattern="InOut" />
                    </camel:otherwise>
                </camel:choice>
            </camel:route>
            <camel:route errorHandlerRef="errorHandler">
            <!-- Route to put message from folder to JMS queue if memory consumption is below limit-->
                <camel:from uri="file:data/inbox"/>
                    <camel:process ref="checkMemoryConsumption"/>
                    <camel:convertBodyTo type="String" />
                    <camel:log message="Sucessfully processing service order ${headers.OrderNumber}-${headers.OrderVersion}.xml"/>
                    <to uri="my queue"
                            pattern="InOut" />
            </camel:route>

答案 1 :(得分:0)

只需保存对您的活动的引用,并在$destroy

中使用它
var myFuncEvent = $scope.$on('event', myFunc);
$scope.$on('$destroy', myFuncEvent);