Angular4:如何检索n-2网址

时间:2017-08-10 16:13:56

标签: angular

url / getEmployees将启动EmployeesComponent,它显示员工的监听, 当我点击员工时,会启动url getEmployee / N,EmployeeComponent, 因此,我希望在验证员工后可以检索url / getEmployees。 它有可能吗?如何?

2 个答案:

答案 0 :(得分:0)

您也可以使用RouterLink导航回来:

<div class='panel-footer'>
    <a class='btn btn-default' routerLink="/getEmployees" >
        <i class='glyphicon glyphicon-chevron-left'></i> Back
    </a>
</div>

答案 1 :(得分:0)

我建立的解决方案是使用存储网址的服务,这里只有一个网址,但它可能是一个数组。 SharedService

    public class StoresOfShopkeeperAdapter extends RecyclerView.Adapter<StoresOfShopkeeperAdapter.MenuTypesViewHolder> {
    protected int lastPosition = -1;
    private List<Store> menuTypesList;
    private Context context;

    public StoresOfShopkeeperAdapter(Context Context, List<Store> contactList) {
        this.menuTypesList = contactList;
        this.context = Context;
    }


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

    @Override
    public void onBindViewHolder(final MenuTypesViewHolder menuTypesViewHolder, final int i) {
        final Store mt = menuTypesList.get(i);
        final int lcount = mt.like;
        menuTypesViewHolder.vName.setText(mt.name);
        Glide.with(context).load("http://test.tiarafood.ir/img/" + mt.image1)
                .bitmapTransform(new RoundedCornersTransformation(context, 0, 0, RoundedCornersTransformation.CornerType.TOP))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.ic_logo)
                .override(500, 500).into(menuTypesViewHolder.vImage);
        if (mt.like > 0)
            menuTypesViewHolder.likeButton.setLiked(true);
        else
            menuTypesViewHolder.likeButton.setLiked(false);
        menuTypesViewHolder.likeCount.setText(String.valueOf(mt.like));
        menuTypesViewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DeleteStoreTask().execute(String.valueOf(mt.id), String.valueOf(i));
            }
        });
        setFadeAnimation(menuTypesViewHolder, i);
        // menuTypesViewHolder.vImage.setImageBitmap(BitmapFactory.decodeByteArray(mt.image, 0, mt.image.length));
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public MenuTypesViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View itemView = LayoutInflater.
                from(viewGroup.getContext()).
                inflate(R.layout.stores_of_shopkeeper_recyclerview_fragment, viewGroup, false);
        final MenuTypesViewHolder holder = new MenuTypesViewHolder(itemView);
        return new MenuTypesViewHolder(itemView);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public void onViewDetachedFromWindow(MenuTypesViewHolder holder) {
        ((MenuTypesViewHolder) holder).itemView.clearAnimation();
    }


    public class MenuTypesViewHolder extends RecyclerView.ViewHolder {

        protected TextView vName, likeCount;
        protected ImageView vImage;
        private Context context;
        protected LikeButton likeButton;
        protected Button btn_delete;

        public MenuTypesViewHolder(View v) {
            super(v);
            vName = (TextView) v.findViewById(R.id.myImageViewText);
            vImage = (ImageView) v.findViewById(R.id.myImageView);
            likeButton = (LikeButton) v.findViewById(R.id.like_button);
            likeCount = (TextView) v.findViewById(R.id.like_count);
            btn_delete = (Button) v.findViewById(R.id.btn_delete_store);
        }
    }


    private void setFadeAnimation(MenuTypesViewHolder view, int position) {
        if (position > lastPosition) {

            AlphaAnimation anim = new AlphaAnimation(0.0f, 2.0f);
            anim.setDuration(1000);
            view.itemView.startAnimation(anim);
            lastPosition = position;
        }
    }

    public class DeleteStoreTask extends AsyncTask<String, String, String> {

        HttpURLConnection conn;
        URL url = null;
        String storeId, i;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... params) {

            storeId = params[0];
            i = params[1];

            try {
                url = new URL("http://www/api/" + storeId + "/deletestore");

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "false";
            }
            try {

                // Setup HttpURLConnection class to send and receive data from php and mysql
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(1000);
                conn.setConnectTimeout(1000);
                conn.setRequestMethod("POST");

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
                return "false";
            }

            try {

                int response_code = conn.getResponseCode();

                // Check if successful connection made
                if (response_code == HttpURLConnection.HTTP_OK) {

                    // Read data sent from server
                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                    StringBuilder result = new StringBuilder();
                    String line;

                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }

                    // Pass data to onPostExecute method
                    return (result.toString());

                } else {
                    return "false";
                }

            } catch (IOException e) {
                e.printStackTrace();
                return "false";
            } finally {
                conn.disconnect();
            }
        }

        @Override
        protected void onPostExecute(String result) {
            if (result != "false") {
                if (menuTypesList.size() == 1) {
                    menuTypesList.clear();
                    notifyItemRemoved(Integer.valueOf(i));

                } else {
                    //Main_ShopkeeperFragment.data.remove(Integer.valueOf(i));
                    menuTypesList.remove(Integer.valueOf(i));
                    //Main_ShopkeeperFragment.ta.notifyDataSetChanged();
                    notifyItemRemoved(Integer.valueOf(i));
                    //notifyItemRangeChanged(0,getItemCount());
                }
            }
        }
    }
}

EmployeeComponent

import { Injectable } from '@angular/core';

@Injectable()
export class SharedService {

  private urlOrigin: string;
  public setUrlOrigin(url: string) :void {this.urlOrigin=url;}
  public getUrlOrigin() {
    let url: string = this.urlOrigin;
    if (url === '')
      url = '/home';
    this.urlOrigin = ''; // remove the url after using
    return url;
  }
  constructor() { }
}