Angular:指令内的变量模板(Angular: Variable template inside directive)
在我的Angular模板中,我使用了一个定语指令,如下所示:
HTML:
<div id="my-template-one" my-template-directive></div>
JS:
// ... .directive('myTemplateDirective', ['myconfig', function (myconfig) { return { templateUrl: myconfig.TEMPLATE_PATH + 'my-template-one.html', controller: function ($scope, $rootScope) { // code }, controllerAs: 'dir' } }]);
为了在另一个页面上包含另一个模板
my-template-two.html
,我想使用相同的指令。 我不想复制指令。 如何将模板作为变量传递?另一页上的HTML:
<div id="my-template-two" my-template-directive></div>
我的目标是以某种方式告诉我的指令在调用此HTML时呈现
my-template-two.html
。In my Angular template I use an attributive directive as follows:
HTML:
<div id="my-template-one" my-template-directive></div>
JS:
// ... .directive('myTemplateDirective', ['myconfig', function (myconfig) { return { templateUrl: myconfig.TEMPLATE_PATH + 'my-template-one.html', controller: function ($scope, $rootScope) { // code }, controllerAs: 'dir' } }]);
For including another template,
my-template-two.html
, on another page, I would like to use the same directive. I do not want to duplicate the directive. How can I pass the template as an variable?HTML on another page:
<div id="my-template-two" my-template-directive></div>
My goal is that somehow I can tell my directive to render
my-template-two.html
when this HTML is called.
原文:https://stackoverflow.com/questions/39249847
最满意答案
“错误”并不意味着错误,它只是意味着返回的对象是一个“幽灵”而没有读入其属性。为关系的另一方获取错误是正常的,因为你没有设置的提取通过其关系从一个不受控制的对象创建级联。
当您访问故障的属性时,它将作为一个功能齐全的对象出现故障。
编辑:
来自评论:
问题是我通过NSLog请求这种关系,但我仍然无法得到关系实体。
不你不是。 您只需要自己请求
Items
实体然后记录它们。 他们按照预期为他们的关系返回错误。 只有当你问每一个关系另一边的实际对象时,你才能保证看到一个对象而不是一个错误。这就是你需要强制关系中对象的错误:
NSLog(@"itemObject.Item_to_item_comments: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row].Item_to_item_comments.someAttribute]);
你还有一个问题就是你正在比较两个单独提取的结果。 这个:
NSLog(@"Results: %@", [mutableItemsFetchResults description]); mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
...在
mutableItemsFetchResults
之前记录mutableItemsFetchResults
。 下一个NSLog(可能)在获取后记录结果。 这意味着您可能会在两种不同的故障状态下查看两组不同的对象。您可能也有问题,因为
mutableItemsFetchResults
显然是一个属性,但您没有使用self.mutableItemsFetchResults
表示法来确保它被正确保留。 另外,我认为你不需要mutableCopy
。A "fault" doesn't mean an error, it just means that the object returned is a "ghost" without its attributes read in. It's normal to get faults for the other side of relationship because you don't what a fetch to set off an uncontrolled cascade of object creation via its relationships.
When you access an attribute of the fault, it will be faulted in as a fully functional object.
Edit:
From comment:
The problem is that I am requesting such relationship through NSLog, but I still can't get the relationship entities.
No you're not. Your just requesting the
Items
entities themselves and then logging them. They are returning faults for their relationships as expected. Only if you ask each for the actual object on the other side of the relationship are you guaranteed to see an object instead of a fault.This is what you need to force the faulting-in of the objects in the relationship:
NSLog(@"itemObject.Item_to_item_comments: %@", [mutableItemsFetchResults objectAtIndex:indexPath.row].Item_to_item_comments.someAttribute]);
You're other problem is that you are comparing the results of two seperate fetches. This:
NSLog(@"Results: %@", [mutableItemsFetchResults description]); mutableItemsFetchResults = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];
... logs the
mutableItemsFetchResults
before the fetch occurs. The next NSLog (presumably) logs the results after the fetch. This means you're looking at possibly two different sets of objects in two different fault states.You may also have a problem because
mutableItemsFetchResults
is apparently a property but you're not using theself.mutableItemsFetchResults
notation to make sure it is properly retained. Also, I don't think you need themutableCopy
.
相关问答
更多-
iPhone SDK的核心数据:获取所有实体与零关系?(iPhone SDK Core Data: Fetch all entities with a nil relationship?)[2022-10-05]
尝试使用零计数,而不是: NSPrdicate *predicate = [NSPredicate predicateWithFormat:@"author == nil || author.@count =0"]; Try using a count of zero instead: NSPrdicate *predicate = [NSPredicate predicateWithFormat:@"author == nil || author.@count =0"]; -
首先,选择一个JSON库来使用,我个人喜欢TouchJSON,但还有其他几个也是非常好的。 复杂的部分虽然不是很难,但是将您的托管对象转换成适合的转换结构。 我写这个真正的快速,所以它可能有一个错误或两个:) 你所说的方法是: - (NSString*)jsonStructureFromManagedObjects:(NSArray*)managedObjects; - (NSArray*)managedObjectsFromJSONStructure:(NSString*)json withManaged ...
-
在任何地方都有可能。 这将非常有帮助。 如果您有联系人ID,则将联系人ID添加到参与者,您可以获得更多与参与相关的信息(如您提到的或不接受等等)。 Have an id everywhere it is possible. It will be very helpful. If you have contact id then add the contact id to participants and you can have more info related to participation (lik ...
-
实现两个核心数据实体之间关系的正常方式与您所指出的完全相同。 这不同于没有建模为“对象图”的普通关系数据库,因为您不需要“连接表”,因为核心数据框架为您管理。 然而,你想模拟一个人际关系 ,所以也许会有一些术语混淆。 在核心数据中,如果您想记录有关关系的其他信息 ,请使用连接表。 例如 Person <-------->> Relationship <<--------> Person 其中Relationship是具有属性type另一个实体,可以进一步描述关系。 The normal way to im ...
-
抱歉延迟更新。 核心数据对于多对多关系并不好,因为它不会创建我想要的索引。 我最终创建了一个RleationManagedObject。 它有一个指向BookManagedObject的属性“book”和一个指向TagManagedObject的属性“tag”。 两个属性都被编入索引(非常重要,默认核心数据不会索引两个属性)。 每次我需要在“book_1”上添加“tag_1”时,我会使用“book_1”和“tag_1”搜索所有RelationManagedObject。 如果没有找到,我创建这个relati ...
-
iPhone开发中心的核心数据入门为我做了诀窍。 :-) 不,严格来说,这看起来似乎很明显,但是这个单页文档列出了一个很好的策略,用于阅读哪些文档和教程以及以何种顺序阅读。 一旦你完成了它推荐的第一个教程,这个奇怪的术语会感觉不那么奇怪。 Getting Started with Core Data in the iPhone Dev Center did the trick for me. :-) No, seriously, it might seem like obvious advice, but ...
-
Coredata在使用coredata保存二进制数据时有一个“允许外部存储”选项,该选项会自动将大于1mb的文件存储到磁盘。 但是,这个问题与这些问题非常相似: https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database 和 将大型(如图像/电影)文件存储在iPhone上的Core Data中 与选定的答案相反,只要没有 ...
-
“错误”并不意味着错误,它只是意味着返回的对象是一个“幽灵”而没有读入其属性。为关系的另一方获取错误是正常的,因为你没有设置的提取通过其关系从一个不受控制的对象创建级联。 当您访问故障的属性时,它将作为一个功能齐全的对象出现故障。 编辑: 来自评论: 问题是我通过NSLog请求这种关系,但我仍然无法得到关系实体。 不你不是。 您只需要自己请求Items实体然后记录它们。 他们按照预期为他们的关系返回错误。 只有当你问每一个关系另一边的实际对象时,你才能保证看到一个对象而不是一个错误。 这就是你需要强制关系中 ...
-
这是一个正常的集合,但核心数据建议不要直接分配一个新集合(出于这个原因)。 如果您使用的是自定义类,则标准实现将包含addFromBundleObject:和removeFromBundleObject:方法供您使用。 如果你不使用自定义类,或者选择不实现这些方法,那么你可以使用mutableSetValueForKey:方法来获得一个你可以修改的集合。 NSMutableSet *mutableSet = [book mutableSetValueForKey:@"fromBundle"]; [mutab ...
-
相同的书页167,“refreshObject:mergeChanges:”会将其转换回故障对象。 Same book page 167, "refreshObject: mergeChanges:" will convert it back to a fault object.