Javascript AngularJS:获取隔离范围中属性定义指令的值

Javascript AngularJS:获取隔离范围中属性定义指令的值,javascript,angularjs,Javascript,Angularjs,因此,我有一个自定义指令,名为例如custom,如下所示: app.directive('custom', function() { return { restrict: 'A', scope: { itemSelector: '=custom', gutter: '=' }, link: function(scope, element, attrs){ console.log("IS: " + scope.i

因此,我有一个自定义指令,名为例如
custom
,如下所示:

app.directive('custom', function()
{
    return {

        restrict: 'A', 
        scope: { itemSelector: '=custom', gutter: '=' }, 
        link: function(scope, element, attrs){
            console.log("IS: " + scope.itemSelector);
            console.log("GUTTER: " + scope.gutter);
        }
    }
}
通过HTML调用,如下所示

<div custom="item" gutter="10"><!--content--></div>

有人能告诉我为什么
scope.gotter=='10'
scope.itemSelector===undefined

是否可以通过这种方式获得定义属性的指令的值


谢谢

我想您的指令的父范围中没有定义项。下面的帖子()中解释了两种解决方案

  • 您希望项作为字符串传递,并在其周围添加单引号(默认情况下,它作为角度表达式计算)

  • 或者更改指令配置,使其将自定义属性视为字符串: 范围:

    {itemSelector:'@custom',gutter:'='}


希望这有帮助

你能做一把小提琴吗?
item
是在作用域上定义的对象吗?“item”只是字符串文字。我希望
scope.itemSelector==“item”
。要读取字符串文字,应使用
@
<代码>范围:{itemSelector:'@custom'}非常感谢,回答得很好。我没意识到这是一个角度表达式。使用“@”配置或单引号非常有效。显然,
gutter=“10”
的工作原理是,可以将
10
作为一个文本整数进行计算。