是否可以为ngx-bootstrap datepicker全局配置日期格式?
文档提到了BsDatepickerConfig类以及如何将它传递给每个单独的日期选择器,但有点惊讶的是,似乎没有可能全局配置它(至少没有记录)
https://valor-software.com/ngx-bootstrap/#/datepicker#bs-datepicker-config
答案 0 :(得分:14)
目前尚未记录,但可以很容易地实现。对于不同的组件(工具提示,弹出窗口等),我们有几个演示,而datepicker的代码几乎相同。如果您要全局配置日期选择器,请将基本BsDatepickerConfig
替换为组件或模块的providers
部分中的基本export function getDatepickerConfig(): BsDatepickerConfig {
return Object.assign(new BsDatepickerConfig(), {
dateInputFormat: 'YYYY-MM-DD'
});
}
@NgModule({
...
providers: [{ provide: BsDatepickerConfig, useFactory: getDatepickerConfig }]
})
。
public class MainActivity extends AppCompatActivity implements
OnMapReadyCallback {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
boolean success =
googleMap.setMapStyle(MapStyleOptions.loadRawResourceStyle(
MainActivity.this,
R.raw.style_json));
if (!success) {
Log.e("google map", "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e("google map", "Can't find style. Error: ", e);
}
mSetUpMap();
}
private static final String TAG = "MapActivity";
private static final String FINE_LOCATION =
Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COURSE_LOCATION =
Manifest.permission.ACCESS_COARSE_LOCATION;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private Boolean mLocationPermissionsGranted = false;
private GoogleMap mMap;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationPermission();
}
private void initMap(){
Log.d(TAG, "initMap: initializing map");
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MainActivity.this);
}
private void getLocationPermission(){
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION};
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = true;
initMap();
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}else{
ActivityCompat.requestPermissions(this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull
String[] permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: called.");
mLocationPermissionsGranted = false;
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE : {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Toast.makeText(getApplicationContext(), "Permission
granted", Toast.LENGTH_SHORT).show();
initMap();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission
denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
private void mSetUpMap() {
// your method code
}
}
示例 - https://stackblitz.com/edit/angular-tvahsw?file=app%2Fapp.module.ts