gpt4 book ai didi

Angularjs 指令和范围(获取字符串而不是对象)

转载 作者:行者123 更新时间:2023-12-02 04:51:39 27 4
gpt4 key购买 nike

大家好,我的指令有问题,我在论坛中搜索了任何解决方案,但没有找到适合我的方法。

我有一个指令,在我的 html 中的一个 ng-repeat 中,看起来像这样

<custom-slide item="{{slides[$index]}}" pos='$index'  ></custom-slide>

我需要以这种方式传递项目,因为我需要观察这个对象的值是否在我的主 Controller 中发生了变化。

我的指令范围是这样的:

 return {
restrict: 'E',
link: linker,
scope:{
item: '@item',
pos: '=pos'
//slide: '@slide',

}

我的问题是当我使用 scope.item 时我得到的是一个字符串而不是一个 objetc。

{type:0, isSelected:'slide' ,param1:'',param2:' ',param3:' '}

但我得到的是字符串,可以通过任何方式将其作为对象,无需解析

有什么建议!!?非常感谢!

编辑

我需要将 item 作为对象,因为在我的链接器中我更改了指令的模板,并且在这个模板中我有类似的东西:

   <div>
<div class="mainText">{{scope.item.param1}}</div>
<div class="footer Text">{{scope.item.param2}}</div>
</div>

所以当 {{scope.item.param1 }} 被评估时,值是未定义的,因为它不是一个对象

最佳答案

尝试从项目属性中删除 {{}}:

<custom-slide item="slides[$index]" pos="$index"></custom-slide>

关于Angularjs 指令和范围(获取字符串而不是对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534091/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com