Javascript AngularJs将HTML中每个ng repeat的实例传递给指令

Javascript AngularJs将HTML中每个ng repeat的实例传递给指令,javascript,css,angularjs,angularjs-directive,angularjs-ng-repeat,Javascript,Css,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我认为这应该很简单,但我遗漏了一些东西。我如何将下面的ng repeat中的flowObj传递给我的指令?我想将其传递给我的指令,然后单击,使用该FlowObj,然后应用一些逻辑。我尝试在指令中使用注释代码 scope: { test:"@" } angular.module('directives', ['opsimut']).directive('flowclick', function() { return { /* scope

我认为这应该很简单,但我遗漏了一些东西。我如何将下面的
ng repeat
中的
flowObj
传递给我的指令?我想将其传递给我的指令,然后单击,使用该
FlowObj
,然后应用一些逻辑。我尝试在指令中使用注释代码

scope: { 
    test:"@" 
}
angular.module('directives', ['opsimut']).directive('flowclick', function() {
    return {   
        /* 
        scope: {
            test:"@"   // set the attribute name on the directive's scope
        },
        */
        link: function(scope, elem, attr) {
            elem.bind('click', function(scope) {
                debugger;
                alert(scope.flowObj);
                //scope.name += '!';
                //scope.$apply();
            });
        }
    };
});
但它似乎把我的CSS搞砸了

HTML:


{{flowObj.name}
这是我的指示

scope: { 
    test:"@" 
}
angular.module('directives', ['opsimut']).directive('flowclick', function() {
    return {   
        /* 
        scope: {
            test:"@"   // set the attribute name on the directive's scope
        },
        */
        link: function(scope, elem, attr) {
            elem.bind('click', function(scope) {
                debugger;
                alert(scope.flowObj);
                //scope.name += '!';
                //scope.$apply();
            });
        }
    };
});
基于答案的解决方案:

angular.module('directives',['opsimut'])。
指令('flowclick',函数(){
返回{
链接:功能(e、元素、属性){
//范围是指指令的范围,
//elem是指令根元素的jquery lite(或jquery full)对象。
//attr是directive元素的属性字典。
元素绑定('click',函数(e1){
调试器;
警报(如flowObj);
},e);
}
};
});

解决方案:从指令中删除整个
范围
属性,所有内容都应按预期工作

另外: 您需要从此行重命名
范围
参数:

elem.bind('click', function(scope) {
例如:

elem.bind('click', function(e) {
因为您正在使用相同的名称覆盖事件处理程序中对
范围的访问

说明:

ng repeat
指令使其每个克隆都有自己的新作用域。由于元素上的指令在默认情况下共享作用域,因此放置在
ng repeat
旁边的任何其他指令共享其作用域,并可以访问当前迭代的
$scope
变量。换句话说,您的自定义指令已经与
ng repeat
共享范围,并且默认情况下可以访问
flowObj

在自定义指令上指定
scope
属性时,它不起作用的原因是,这导致指令具有自己的隔离作用域,而该隔离作用域不与
ng repeat
共享,因此您无法访问克隆作用域上的变量