首页 \ 问答 \ Angular:指令内的变量模板(Angular: Variable template inside directive)

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
更新时间:2023-07-26 19:07

最满意答案

“错误”并不意味着错误,它只是意味着返回的对象是一个“幽灵”而没有读入其属性。为关系的另一方获取错误是正常的,因为你没有设置的提取通过其关系从一个不受控制的对象创建级联。

当您访问故障的属性时,它将作为一个功能齐全的对象出现故障。

编辑:

来自评论:

问题是我通过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 the self.mutableItemsFetchResults notation to make sure it is properly retained. Also, I don't think you need themutableCopy.

相关问答

更多
  • 尝试使用零计数,而不是: 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.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)