I am following this blog post in creating directives: http://joelhooks.com/blog/2014/02/11/lets-make-full-ass-angularjs-directives/
He seemed to recommend having the controller seperate from the directive. However trying to run a function from the external controller in the link function results in undefined is not a function.
Controller
wrmcControllers.controller 'voteCtrl', ($scope,$attrs) ->
this.hasVoted = false
this.init = ->
if this.hasVoted
#scope.vote = Vote.get(scope.vote)
else
this.vote = null
this.voteUp = ->
if this.vote == true
this.vote = null
else
this.vote = true
this.hasVoted = true
this.voteDown = ->
if this.vote == false
this.vote = null
else
this.vote = false
this.hasVoted = true
Directive
wrmcDirectives.directive 'voteSet', ->
restrict: 'AE'
scope: {}
controller: 'voteCtrl'
templateUrl: 'vote.html'
link: (scope,element,attrs,controller) ->
console.log controller
controller.init()
Console.log controller output
function () {
if (this.vote === false) {
return this.vote = null;
} else {
this.vote = false;
return this.hasVoted = true;
}
}
$scope.inittothis.init- PSLvoteDownas if that is the entire object. - Claies