在* ngIf中使用“ OR”

时间:2018-09-21 11:52:37

标签: angular angular-ng-if ngif

我有一个来自API的响应,我想设置一个if条件来检查响应是3还是6,以将ng-template设置为成功,否则将获取的任何其他数据设置为危险,< / p>

如何检查data.delivery_status是3还是6?

这是我下面的代码,该代码不起作用:

package com.example.com

import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

public class GirisAktivitesi extends AppCompatActivity {


    TextView signup, yaziuyegirisi;
    EditText username, userpassword;
    Button btnLogin;

    private static final String loginUrl = "URL";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_giris);
        yaziuyegirisi = findViewById(R.id.yaziuyegirisi);
        username = findViewById(R.id.username);
        userpassword = findViewById(R.id.userpassword);
        signup = findViewById(R.id.usersignup);

        btnLogin = findViewById(R.id.btnUserLogin);



        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GirisAktivitesi.this, YeniKayitAktivitesi.class);
                startActivity(intent);
            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userLogin();
            }
        });
    }

    public void userLogin() {
        StringRequest request = new StringRequest(StringRequest.Method.POST, loginUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                try {

                    JSONArray jsonResponse = new JSONArray(response);
                    String registerResultString = jsonResponse.getJSONObject(0).getString("status");
                    String registerMessageString = jsonResponse.getJSONObject(0).getString("message");

                    if (registerResultString.equals("true")) {

                        Toast.makeText(getApplicationContext(), "Sorgulanıyor..", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(getApplicationContext(), DashboardAktivitesi.class); 
                        startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(), "Entered is unfortunately incorrect", Toast.LENGTH_LONG).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>(); 
                params.put("email", username.getText().toString().trim());
                params.put("password", userpassword.getText().toString().trim()); 
                return params;
            }
        };
        Volley.newRequestQueue(this).add(request);
    }
}

6 个答案:

答案 0 :(得分:2)

由于这些选项是互斥的,因此只需两次添加li元素(两个版本)即可:

<li *ngIf="data.delivery_status==='3'">
    <ng-template #success>
        <td><span class="btn btn-success btn-sm">Sent</span></td>
    </ng-template>
</li>

<li *ngIf="data.delivery_status==='6'">
<ng-template #danger>
    <td><span class="btn btn-danger btn-sm">Sent</span></td>
</ng-template>
</li>

Angular将仅渲染其中之一,具体取决于delivery_status的值。

答案 1 :(得分:2)

您可以在下面简单地做

*ngIf="(data.delivery_status === 3 || data.delivery_status === 6);then success else danger"

完整代码

<li *ngIf="(data.delivery_status === 3 || data.delivery_status === 6);then success else danger"></li>
<ng-template #success>
  <td><span class="btn btn-success btn-sm">Sent - success</span></td>
</ng-template>
<ng-template #danger>
  <td><span class="btn btn-danger btn-sm">Sent - danger</span></td>
</ng-template>

工作Stackblitz

答案 2 :(得分:1)

使用*ngIf="data.delivery_status==='3' || data.delivery_status === '6'代替OR '6'

<li *ngIf="data.delivery_status==='3' || data.delivery_status === '6';then success else danger"></li>
<ng-template #success>
    <td><span class="btn btn-success btn-sm">Sent</span></td>
</ng-template>
<ng-template #danger>
    <td><span class="btn btn-danger btn-sm">Sent</span></td>
</ng-template>

答案 3 :(得分:0)

<li *ngIf="data.delivery_status==='3' || data.delivery_status==='6';then success else danger"></li>

但更好的方法是将该条件提取到组件中。

答案 4 :(得分:0)

将以下代码用于OR条件

<li *ngIf="data.delivery_status ==='3' || data.delivery_status === '6';then success else danger"></li>

答案 5 :(得分:0)

我了解到您希望状态为'3'或'6'时使用Relation,但是所有其他情况均为User.connection.execute("YOUR SQL HERE...") 。所以这是我的代码:

success

这也是使用NgSwitch的好机会。

  

请参阅:https://angular.io/api/common/NgSwitch

这是我的编码建议:

danger