在我的Angular2模板中,我有以下绑定:
{{Stringify(result)}}
{{result.MyProperty}}
Stringify
是一个返回输入对象JSON.stringify
的函数。 Stringify
函数返回了一个JSON字符串,显示MyProperty
的名称和值。
然而,第二行返回
类型错误。无法阅读财产' MyProperty'在{{result.MyProperty}}中未定义。
JSON.stringify
清楚地显示此属性/字段存在,为什么我收到错误?
答案 0 :(得分:1)
如果它可以被访问,JS就不会抛出
尝试改为
{{result?.MyProperty}}
也许Angular尝试在result.MyProperty
具有值result
而Stringify(result)
没有阻塞null
之前访问result
。
在此期间更新- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeLocationServices];
}
-(void)initializeLocationServices
{
NSLog(@"Started location services");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone;
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_locationManager.pausesLocationUpdatesAutomatically = NO;
[_locationManager startUpdatingLocation]; // to show authorisation popup
}
-(CLCircularRegion*)createRegion
{
// Test coordinates
CLLocationDegrees latitude = 12.908768;
CLLocationDegrees longitude = 77.652061;
CLLocationDistance radius = 50; // meters;
// If radius is too large, registration fails automatically, so limit the radius to the maximum value
if (radius > _locationManager.maximumRegionMonitoringDistance) {
radius = _locationManager.maximumRegionMonitoringDistance;
}
CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(latitude, longitude) radius:radius identifier:@"TEST"];
region.notifyOnEntry = YES;
region.notifyOnExit = YES;
NSLog(@"Created region");
return region;
}
-(void)monitorProximity
{
CLRegion *region = [self createRegion];
// Check if support is unavailable
if ( ![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]]) {
NSLog( @"Failed to initialise region monitoring: support unavailable");
return;
}
// Check if authorised
if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorized) {
NSLog( @"Failed to initialise region monitoring: app not authorized to use location services");
return;
} else
{
NSLog(@"Started monitoring proximity");
}
// Clear out any old regions to prevent buildup.
if ([_locationManager.monitoredRegions count] > 0)
{
for (id obj in _locationManager.monitoredRegions)
[_locationManager stopMonitoringForRegion:obj];
}
[_locationManager startMonitoringForRegion:region];
}
-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
NSLog(@"Started monitoring for region: %@", [region description]);
[_locationManager requestStateForRegion:region]; // check if already inside region
}
-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
NSLog(@"Failed to start monitoring for region: %@", [error localizedDescription]);
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Failed Monitoring" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
NSLog(@"didDetermineState");
if (state == CLRegionStateInside) {
NSLog(@"inside");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"inside" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return;
} else if (state == CLRegionStateOutside) {
NSLog(@"outside");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"outside" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
} else {
NSLog(@"unknown");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"unknown" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
NSLog(@"didEnterRegion");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"did enter region" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
NSLog(@"didExitRegion");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"did exit region" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
NSLog(@"Monitoring authorisation status is now: %@", status == kCLAuthorizationStatusAuthorized ? @"authorized" : @"not authorized");
if (status == kCLAuthorizationStatusAuthorized) {
[self monitorProximity];
}
}
时(可能是因为从服务器收到了值,视图会在您识别出之前显示空字符串之前更新。)
您的问题没有提供足够的背景知识。
另见