Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  197] [ 5]  / answers: 1 / hits: 36189  / 12 Years ago, mon, january 14, 2013, 12:00:00

I'm using AngularJS in a project of mine, and I wanted to try creating directives. I already followed several tutorials and I can't see where I'm doing it wrong. Even worst, it doesn't shows up any error or warning message, but it also does not executes the directive's function. Right now, my code is pretty much this:



angular.module('components', []).directive('ngxOnshow', function() {
return {
restrict: 'A',
link: function(scope, element, attrs){
console.log(hello world)
//Resto do código da função
}
};
});
var module = angular.module('app', ['components']);


In the body of the HTML page I have this:



<body ng-autobind ng-app=app>


But then, when I use the directive, it does not work.



<div ng-show=showApp == true ngx-onshow=showAppBar()>
</div>


The rest of the application works just fine, the bindings, the default directives, everything except this. Perhaps I'm missing something?



Thanks,
Scorch :)


More From » angularjs

 Answers
3

Use '&' to allow a directive to execute an expression/function defined in the parent scope. $observe an interpolated isolate scope attribute (i.e., one defined with '@' that contains {{}}) to determine when it changes:



myApp.directive('ngxOnshow', function () {
return {
restrict: 'A',
scope: {
showApp: '@',
ngxOnshow: '&'
},
link: function (scope, element, attrs) {
console.log(inside link function)
attrs.$observe('showApp', function (newValue) {
newValue === true && scope.ngxOnshow()
})
}
};
});


newValue is compared with true not true because interpolated values are always strings.



HTML:



<div ng-show=showApp == true show-app={{showApp}} ngx-Onshow=showAppBar()>


Fiddle.


[#80880] Sunday, January 13, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lincolnx

Total Points: 602
Total Questions: 90
Total Answers: 94

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
;