简体   繁体   English

Angular-数据更新后运行jQuery

[英]Angular - Run jQuery after data update

I'm using Angular 1.4.7. 我正在使用Angular 1.4.7。 I didn't set up this site so I'm not sure what information I need to give for this. 我没有设置此站点,所以不确定是否需要为此提供什么信息。 We're rendering some search filters, but whether or not they're enabled is configured by data retrieved from the server, for example: 我们正在渲染一些搜索过滤器,但是是否启用它们是由从服务器检索的数据配置的,例如:

$scope.searchFields = {
    "date": true,
    "price": true,
    ...
};

The HTML for a search field ( $scope.filter being an individual set of vars for this particular filter, in this case it's just selecting a maximum numeric value): 搜索字段的HTML( $scope.filter是此特定过滤器的一组变量,在这种情况下,它只是选择一个最大值):

<div class="limit-group clearfix">
    <span class="detail">Filter Name:</span>
    <div class="filter-select-small">
        <select name="filter" class="custom-select" ng-if="searchFilters.code">
            <option value="">No Max</option>
            <option ng-repeat="(key, value) in filter" value="<%key%>"><%value%></option>
        </select>
        <span class="unavailable" ng-if="!searchFilters.code">Unavailable</span>
    </div>
</div>

There are conditions where the available search fields will change. 在某些情况下,可用的搜索字段将更改。 The select fields, for whatever reason, get replaced by a jQuery plugin called selectbox. 无论出于何种原因,选择字段都会被称为selectbox的jQuery插件所取代。 I have a function resetFilterStyles() which reattaches the jQuery plugin to the fields, but I'm not sure where to fire it. 我有一个resetFilterStyles()函数, resetFilterStyles() jQuery插件重新附加到字段,但是我不确定在哪里触发它。

Inside of methods defined on $scope the data is being updated using $http.get() but running resetFilterStyles() inside of these anonymous functions does not work, presumably because Angular hasn't processed yet that the data has been updated, so the changes that would be performed by resetFilterStyles() are undone by Angular's updates to the DOM. $scope定义的方法内部,正在使用$http.get()更新数据,但是在这些匿名函数内部运行resetFilterStyles()无效,大概是因为Angular尚未处理数据,因此Angular对DOM的更新将撤消resetFilterStyles()将要执行的更改。

I've tried setting a $watch handler but this seems to be the wrong place to run my function as it doesn't appear to take, either (and additionally causes grievous errors within Angular). 我试过设置一个$watch处理程序,但这似乎是运行我的函数的错误位置,因为它似乎也没有用(并且还会在Angular中引起严重的错误)。 As someone who doesn't use Angular, I'm not sure where to go from here. 作为不使用Angular的人,我不确定从这里去哪里。

Edit 编辑

The reset function: 重置功能:

function resetFilterStyles() {
    // desktop adv filters
    var filterContainer = $('.advance-filter-dropdown');
    $('select',filterContainer).selectbox('detach');
    $('select',filterContainer).selectbox('attach');
    $(".filter-select-small .sbOptions").niceScroll({cursorborder:"",cursorcolor:"#ccc",autohidemode: false});

    // mobile adv filters
    var filterContainer = $('.filter-wizard-mobile');
    $('select',filterContainer).selectbox('detach');
    $('select',filterContainer).selectbox('attach');
    $(".filter-select-small .sbOptions").niceScroll({cursorborder:"",cursorcolor:"#ccc",autohidemode: false});
}

From the AngularJS perspective, you shouldn't do DOM modifications inside the controller. 从AngularJS的角度来看,您不应该在控制器内部进行DOM修改。 You should create a directive/component to handle that for you. 您应该创建一个指令/组件来为您处理。

But if you really must have that jQuery (legacy projects, for example), you can put your DOM modification into $timeout: 但是,如果您确实必须拥有该jQuery(例如旧项目),则可以将DOM修改放入$ timeout中:

$http.get("url").then(function() {
        $timeout(function() {
          // updateDom
        }, 0);
});

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

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