我在想這應該很簡單,但我錯過了一些東西。如何將我的ng-repeat
中的flowObj
通過我的指令?我想將它傳遞給我的指令,然後點擊使用FlowObj
然後應用一些邏輯。我嘗試使用我的指令中的註釋代碼AngularJs將HTML中每個ng-repeat的實例傳遞給指令
scope: {
test:"@"
}
但它似乎搞砸了我的CSS。
HTML:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="center_outer">
<div id="center_inner" ng-controller="CtrlPageFlow">
<div flowclick class="cflow" ng-repeat="flowObj in flows" >
{{flowObj.name}}
</div>
</div>
</div>
</body>
</html>
這裏是我的指令
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']).
directive('flowclick', function() {
return {
link: function(e, elem, attr) {
// scope is the directive's scope,
// elem is a jquery lite (or jquery full) object for the directive root element.
// attr is a dictionary of attributes on the directive element.
elem.bind('click', function(e1) {
debugger;
alert(e.flowObj);
},e);
}
};
});