javascript - AngularJS: function is not a function -


i have angularjs app, , on 1 of pages, have number of widgets, each 1 displaying information status of part of system. working on adding functionality allow user 'hide' heading of given widget.

there 'settings' button on page widgets displayed, which, when clicked, overlays toolbar on top of each of widgets. toolbar has number of buttons- 1 of 'settings' button, opens dialog allows user change settings particular widget.

i have added checkbox dialog, enable user 'hide' heading particular widget view:

configure item dialog

when checkbox selected on dialog, , user clicks 'preview', expecting (eventually- i'm still working on implementation of feature) heading particular widget hidden. however, currently, when user clicks 'preview', whether checkbox selected or not, getting error in console says:

typeerror: $scope.widget.togglewidgetheading not function

this error coming $scope.preview function in ctrl.js called when 'preview' button clicked on dialog:

}).controller('widgetpickerctrl', function($scope, $timeout, fxtag, gpresets, appwidget) {     ...     $scope.preview = function(e) {         $scope.widget.togglewidgetheading();         ...     };     ... }); 

i don't understand why i'm getting console error, since togglewidgetheading() function...

if right-click on function call above in sublime, , select 'go definition', taken directive.js file function defined:

.directive('appwidget', function($timeout, fxtag, appcolorfilter) {     return {         ...         link: function($scope, $element){             ...             var togglewidgetheading = function(){                 ...             }             ...         }     } }) 

also, clicking 'preview' button on dialog no longer closes dialog...

why i'm being told function call not function when defined one? issue here scope (i.e. fact i'm calling function ctrl.js, though it's defined in directive.js)?

the definition of directive, added ..., relevant part directives scopes.

directives can implement several kind of scopes. can inherit , access parent scope, or can have isolated scope example. may read in official documentation explained:

https://docs.angularjs.org/guide/directive

however, whatever scope use, default angularjs implements inheritance of scopes, inheritance works: children can access parent methods, parent cannot access children's methods.

here seems parent scope (the controller) trying access directive's scope, no possible. (even if in link function define togglewidgetheading private variable, , not associated $scope - won't work either).

so have few options in these cases:

  1. define "visible properties" inside service , inject service inside directive , controller. use service in order access , change these values, sync between controller , directive
  2. add scope parameter directive callback & , provide function controller returns chosen visibility of widget, directive can call function , value of widget's visibility
  3. add scope parameter 2 way data binding = in directive, bound widget's visibility of controller, have sync between controller , directive
  4. use events in order communicate between controller , directive, broadcasting event controller when visibility changes, , reading event directive getting widget's visibility value

i hope makes sense


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -