everyone!
I'm an angular newbie that is going crazy with an apparently simple issue. I'm creating a directive that is nothing more than an "ul/li" that can become big. I want this directive to be filled with data from controller.
I can do almost everything, but not two simple and related tasks:
Show a string property inside the "li".- Pass the "li" item as a parameter to the function in the controller (click in some "li")
How you guys can see, it iterates thru the object, print the correct numbers of objects, but the "item" is undefined, so I can't pass it as a function parameter or print inside the list.
Frustrated...
var app = angular.module("app",[]);
app.controller("AppCtrl", function($scope) {
$scope.someFunction = function(item) {
alert("Hello, " + item);
};
$scope.itens = [
{id:1,name:'Lorem'},
{id:2,name:'Ipsum'},
{id:3,name:'Dolor'}
];
});
app.directive("someDirective", function() {
return {
restrict: "E",
// transclude: true,
scope: {
test: "&",
loop: "="
},
template: "<ul><li ng-repeat='item in loop' ng-click='encapsulated(item.name)'>{{item.name}}</li></ul>",
link: function(scope, element, attrs, controller, transcludeFn) {
scope.encapsulated = function(x) {
// this function call doesn't work with a parameter
scope.test(x);
}
}
};
});
li {
color:red;
background:yellow;
margin-bottom:5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="AppCtrl">
<some-directive test="someFunction()" loop="itens"></some-directive>
</div>
Thanks in advance!