I am using CoffeeScript. I have created a directive to warp a jquery ui datepicker control. I want to be able to use this directive in multiple controllers however I can only get it to work if I add it to every coffeescript file for each view I use as shown below.
$ = jQuery
adminApp = angular.module("adminApp", [])
adminApp.controller "OrganizationCreateCtrl", ($scope) ->
$scope.create = () ->
$.ajax 'CreateOrganization',
type: 'POST'
dataType: 'json'
data:
organizationName: $scope.orgName
error: (jqXHR, textStatus, errorThrown) ->
$scope.success = ''
$scope.isSuccess = false
$scope.error = 'Could not generate Organization'
$scope.isError = true
$scope.$apply()
success: (data, textStatus, jqXHR) ->
if data.Success is true
$scope.success = 'Organization Created Succefully. Organization ID: ' + data.Id
$scope.isSuccess = true
$scope.isError = false
$scope.error = ''
$scope.$apply()
else
$scope.success = ''
$scope.isSuccess = false
$scope.error = 'Could not generate Organization'
$scope.isError = true
$scope.$apply()
adminApp.directive 'datepicker', ->
restrict: 'A'
require: 'ngModel',
link: (scope, element, attrs, ngModelCtrl) ->
$ ->
element.datepicker
dateFormat: "yy-mm-dd"
onSelect: (date) ->
ngModelCtrl.$setViewValue date
scope.$apply()
I want to be able to use this directive without including it in every controller I create but can't seem to get it to work.
I would like to create an external coffeescript file with just the directive and use it on the view page that this coffeescript applies to but can't seem to get it to work.
Again this seems like it should be easy but nothing I've tired has worked despite searching for the answer a great length.
Bottom line, how can I more the directive to an external coffeescript file and use it in any controller in my application rather than having to have to add it to every controller file I create.