当我在angular中作为文字字符串传递时,为什么未定义'<'绑定的currentValue

时间:2019-04-26 16:26:20

标签: angularjs

你好,我有一个约束说说itemType:

这是我在组件中定义的方式:

 bindings: {    
     itemType: '<?',
 }

现在由于某种原因,当我将其作为文字字符串而不是属性值传递时,该值未传递,这就是我的意思,让我说我有一个控制器:

someController = function someController( $scope) {
            var self = this;

            self.itemType = 'someString';

工作:

<some-component>
        open-id="singleSelect"
        item-type="ctrl.itemType"

// ..

 itemType.currentValue = 'someString'

不起作用:

<some-component>
        open-id="singleSelect""
        item-type="someString"

// ..

itemType.currentValue = undefined

2 个答案:

答案 0 :(得分:3)

在为单向('<')绑定的组件属性提供文字字符串时,请使用引号:

<some-component
    open-id="singleSelect"
    ̶i̶t̶e̶m̶-̶t̶y̶p̶e̶=̶"̶s̶o̶m̶e̶S̶t̶r̶i̶n̶g̶"̶ ̶
    item-type="'someString'" >
</some-component>

否则,将someString视为属性标识符。它绑定undefined,因为该属性在范围上不存在。

答案 1 :(得分:0)

对字符串使用@绑定。

<绑定期望参数为$scope属性,并且$scope.someString不存在,但$scope.ctrl.itemType确实存在。