指令并不总是在页面加载时触发

时间:2018-01-14 00:48:04

标签: javascript angularjs

我有一个指令,它抓住特定元素的高度并将其应用于另一个元素。

但由于某种原因,它并不总是在页面加载时触发。我想也许有些情况下元素加载时间太长,而且只有在完全加载后才处理它可能是一个更聪明的想法。它似乎适用于页面调整大小(有时)。

为什么会这样,我该如何解决?

例如:

<div class="header">
            <div class="header-logo">
                <a href="http://www.#.com">
                    <img id="header-logo" src="https://placehold.it/200x100">
                </a>
            </div>
            <div class="header-nav">
                <span class="nav-container">
                    <a class="nav-list1" href="http://www.#.com/home">Home</a>
                    <a class="nav-list1" href="http://www.#.com/home">About</a>
                    <a class="nav-list1" href="http://www.#.com/home">Services</a>
                    <a class="nav-list2" href="http://www.#.com/home">Contact</a>
                </span>
            </div>
        </div>
        <div class="section1">
            <div id="overlay">
                <h1>header</h1>
            </div>
        </div>

指令

public class MesesAdapter extends RecyclerView.Adapter{


private List<String> mesesList;
private Activity context = null;


public MesesAdapter(Activity context) {
    this.context = context;
    String[] meses = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
            "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
    mesesList = Arrays.asList(meses);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context)
            .inflate(R.layout.meses, parent, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {

    ViewHolder holder = (ViewHolder) viewHolder;

    String mes = mesesList.get(position);

    holder.meses.setText(mes);

    if (position % 2 == 0)
        holder.linearLayout.setBackgroundColor(Color.GRAY);
}

@Override
public int getItemCount() {
    return mesesList.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.mes)
    TextView meses;
    @BindView(R.id.linear)
    LinearLayout linearLayout;

    ViewHolder(View view) {
        super(view);
        ButterKnife.bind(this, view);
    }
}}

1 个答案:

答案 0 :(得分:1)

请你把你的度量函数放在$ watch函数中,然后是scope。$ apply()。