我使用for循环将数组的所有元素放在vm.klassen
中。我使用console.log来查看是否所有元素都放在vm.klassen
中,就是这种情况。
我试图在前端显示结果,但它只显示最后的结果。
For loop:
if (vm.tijdvak.klassen.length > 1) {
for (i = 0; i < vm.tijdvak.klassen.length; i++) {
vm.klassen = vm.tijdvak.klassen[i].naam
console.log(vm.klassen)
}
}
HTML:
<p ng-if="vm.klassen">Klassen: <pre ng-if="vm.klassen">Namen: {{ vm.klassen }}</pre></p>
我做错了什么,为什么它只显示最后的结果,而它显示了console.log中的每一个结果?
答案 0 :(得分:1)
它显示的是最后一个,因为您正在分配当前索引naam
vm.klassen = vm.tijdvak.klassen[i].naam
它将取代之前的naam
您需要的只是 ng-repeat
喜欢这个
<div ng-repeat="klassen in vm.tijdvak.klassen">
{{klassen.naam}}
</div>
答案 1 :(得分:0)
您在每个循环周期中覆盖public class ServicingActivity extends AppCompatActivity {
public static String url;
SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_servicing);
init();
}
public void init(){
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Serv_OverviewFragment(), "Overview");
adapter.addFragment(new Serv_BaseDataFragment(), "Base Data");
adapter.addFragment(new ServicingFragment(), "Servicing");
adapter.addFragment(new ReplacementFragment(), "Replacement");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
/*public void save_final(){
Serv_BaseDataFragment baseFragment = (Serv_BaseDataFragment)getSupportFragmentManager().findFragmentById(R.id.baseDataTab);
baseFragment.serv_insertToBaseData_db();
ServicingFragment serviceFragment = (ServicingFragment)getSupportFragmentManager().findFragmentById(R.id.servicingTab);
serviceFragment.insertToServicing_db();
ReplacementFragment replaceFragment = (ReplacementFragment)getSupportFragmentManager().findFragmentById(R.id.baseDataTab);
replaceFragment.insertToReplacement_db();
Toast.makeText(this, "ALL DATA STORED SUCCESSFULLY", Toast.LENGTH_LONG).show();
Intent intent = getIntent();
finish();
startActivity(intent);
}*/
public void writePdf(){
String filename = Serv_BaseDataFragment.sysaid.getText().toString();
Serv_CreatePDF fop = new Serv_CreatePDF();
if (fop.writepdf(filename)) {
Toast.makeText(getApplicationContext(), filename + ".pdf created", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_maintenance, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, Serv_SettingsActivity.class);
startActivity(intent);
return true;
}
if (id == R.id.save_tickets) {
//save_final();
writePdf();
return true;
}
return super.onOptionsItemSelected(item);
}
}
。如果您希望vm.klassen
成为名称数组,则可以执行以下操作:vm.klassen
。当然,在for-loop之前声明vm.klassen.push(vm.tijdvak.klassen[i].naam)
是一个数组:vm.klassen