Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单元测试Karma Jasmine SyntaxError:&&引用;角度定向绑定_Javascript_Angularjs_Angularjs Directive_Karma Jasmine - Fatal编程技术网

Javascript 单元测试Karma Jasmine SyntaxError:&&引用;角度定向绑定

Javascript 单元测试Karma Jasmine SyntaxError:&&引用;角度定向绑定,javascript,angularjs,angularjs-directive,karma-jasmine,Javascript,Angularjs,Angularjs Directive,Karma Jasmine,我在我的指令行中得到了SyntaxError:Parse error,我想使用来自父指令方法的“&”单向绑定 myApp.directive('datasourceDeleteBtn', [function() { return { restrict: 'E', replace: true, template: '<a href="#">&#x2715</a>', scope: { datasourceIndex:

我在我的指令行中得到了
SyntaxError:Parse error
,我想使用来自父指令方法的“&”单向绑定

myApp.directive('datasourceDeleteBtn', [function() {
return {
    restrict: 'E',
    replace: true,
    template: '<a href="#">&#x2715</a>',
    scope: {
        datasourceIndex: '@',
        removeParentDiv: '&'
    },
    link: link
};

function link(scope, element, attr) {

    element.bind('click', function(event) {
        event.preventDefault();
        scope.deleteDatasource(scope.datasourceIndex);
    });

    // Notify parent directive
    scope.deleteDatasource = function(datasource_index) {
        // conditional stuff that happens not included
        // {} required for passing values to "&" parent scope method
        scope.removeParentDiv({datasource_index});
    };
}
}]);
问题似乎是茉莉不喜欢{}。删除括号将导致测试通过(由于&requires{}而出现典型错误)

由于在中以错误格式传递json,因此出现错误 方法

您尚未从指令中正确调用在指令作用域
removeParentDiv:'&'
中传递的方法。因为您只做了
scope.removeParentDiv({datasource\u index})
不会将索引参数传递给方法

为了让它工作,你需要做一些改变

  • 应使用指令元素方法

    remove-parent-div="removeParentDiv(index)"
    
  • 当从指令调用它时,通过json结构,
    index
    只是参数,
    datasource\u index
    是它的值

    scope.removeParentDiv({index: datasource_index});
    
  • 调用
    scope.deleteDatasource(scope.datasourceIndex)后运行摘要循环单击
    事件中的code>方法,以便更新
    范围
    绑定

    element.bind('click', function(event) {
        event.preventDefault();
        scope.deleteDatasource(scope.datasourceIndex);
        scope.$apply(); //to run digest cycle, to keep binding in sync
    });
    

  • 谢谢我很好奇为什么需要在“&”单向绑定中传递对象{index:}。当我双向使用“=”时,如果没有这个对象语法,它可以正常工作。看起来my parentDiv函数需要更多的代码才能理解它何时得到{}而不是普通的索引字符串。@liquified在传递
    &
    方法的方法参数时,您需要遵循json结构。是的,当您有
    @
    (单向绑定)时,我们将值从其属性传递给指令,当您从指令的外部上下文中更改它时,该值将在指令中更新,但从指令中更新该值不会(更新父项中的值)该值已更改。这就是为什么它被称为单向绑定。这就是代码不使用
    @
    的原因,但当您使用
    =
    双向绑定时,它会更新两侧的值,所以这就是它在该实例中工作的原因
    scope.removeParentDiv({index: datasource_index});
    
    element.bind('click', function(event) {
        event.preventDefault();
        scope.deleteDatasource(scope.datasourceIndex);
        scope.$apply(); //to run digest cycle, to keep binding in sync
    });