在ng-click中内联if语句以运行两个函数之一

时间:2015-10-28 11:19:10

标签: angularjs

有可能有类似的东西:

public class GridAdapter extends FragmentGridPagerAdapter {

    private final Context mContext;
    private List <Row> mRows;

    public GridAdapter(Context ctx, FragmentManager fm, DataMap dm) {

        super(fm);
        mContext = ctx;

        mRows = new ArrayList <Row>();

        Row row = new Row();

        int position = 0;

        for(DataMap attendee : dm.getDataMapArrayList("array")) {
            AFragment af = new AFragment();
            dm.putInt("tag", position);
            af.setArguments(dm.toBundle());

            row.add(af);
            position++;
        }

        mRows.add(row);

    }


    private Fragment cardFragment(int titleRes, int textRes) {
        Resources res = mContext.getResources();
        CardFragment fragment =
                CardFragment.create(res.getText(titleRes), res.getText(textRes));
        // Add some extra bottom margin to leave room for the page indicator
        fragment.setCardMarginBottom(
                res.getDimensionPixelSize(R.dimen.card_margin_bottom));
        return fragment;
    }

    private class Row {

        final List<Fragment> columns = new ArrayList<Fragment>();

        public Row(Fragment... fragments) {
            for (Fragment f : fragments) {
                add(f);
            }
        }

        public void add(Fragment f) {
            columns.add(f);
        }

        Fragment getColumn(int i) {
            return columns.get(i);
        }

        public int getColumnCount() {
            return columns.size();
        }
    }

    @Override
    public Fragment getFragment(int row, int col) {
        Row adapterRow = mRows.get(row);
        return adapterRow.getColumn(col);
    }

    @Override
    public int getRowCount() {
        return mRows.size();
    }

    @Override
    public int getColumnCount(int rowNum) {
        return mRows.get(rowNum).getColumnCount();
    }

    }

因此,<div ng-click="someVar == '1' ? functionOne() : functionTwo() "> Click me! </div> 取决于someVarng-click还是functionOne

2 个答案:

答案 0 :(得分:3)

我建议为此创建“处理程序”:

<div ng-click="functionThree(someVar)">
  Click me!
</div>

function functionThree(someVar){
    someVar == '1' ? functionOne() : functionTwo(); 
}

答案 1 :(得分:0)

是的,你可以做到。

<div ng-click="obj.length>1  ? functionOne() : functionTwo() ">
   Click me!
</div>