{"id":20314364,"url":"https://github.com/normandy72/controllers-inside-directives","last_synced_at":"2026-04-29T12:40:15.621Z","repository":{"id":153902740,"uuid":"587353532","full_name":"Normandy72/Controllers-Inside-Directives","owner":"Normandy72","description":"Using Controllers Inside Directives. Coursera course \"Single Page Web Applications with AngularJS\" by Yaakov Chaikin.","archived":false,"fork":false,"pushed_at":"2023-01-10T16:11:18.000Z","size":68,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-14T12:46:53.608Z","etag":null,"topics":["angular","angularjs","css","css3","html","html5","javascript","js"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Normandy72.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-10T14:58:27.000Z","updated_at":"2023-01-10T16:11:44.000Z","dependencies_parsed_at":null,"dependency_job_id":"c959a119-4e65-4bee-a5b6-9c46dd9d5cb2","html_url":"https://github.com/Normandy72/Controllers-Inside-Directives","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FControllers-Inside-Directives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FControllers-Inside-Directives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FControllers-Inside-Directives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FControllers-Inside-Directives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Normandy72","download_url":"https://codeload.github.com/Normandy72/Controllers-Inside-Directives/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241818842,"owners_count":20025208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angularjs","css","css3","html","html5","javascript","js"],"created_at":"2024-11-14T18:14:58.262Z","updated_at":"2026-04-29T12:40:15.581Z","avatar_url":"https://github.com/Normandy72.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Using Controllers Inside Directives\n### Step 1: Declare Controller in Directive\n```\nfunction MyDirective(){\n    var ddo = {\n        scope: {\n            prop: '='\n        },\n        controller: ControllerFunction,\n        bindToController: true,\n        controllerAs: 'myCtrl',\n        templateUrl: \"template.html\"\n    };\n    return ddo;\n}\n```\n`bindToController: true` - attach declared scope properties to controller instance instead of directly to $scope\n\n`controllerAs: 'myCtrl'` - use 'myCtrl' in directive's template to refer to controller instance\n\n### Step 2: Define Controller\n```\nControllerFunction.$inject = ['Service'];\nfunction ControllerFunction(Service){\n    var myCtrl = this;\n    myCtrl.method = function(){\n        var name = \"Hello \" + myCtrl.prop;\n        ...\n    };\n}\n```\n`var myCtrl = this;` - attach other properties to 'this' as usual\n\n`myCtrl.prop` - use (\u0026 manipulate) props in isolate scope\n\n### Step 3: Use In Directive's Template\n```\n\u003cdiv ng-if=\"myCtrl.method()\"\u003e\n    {{myCtrl.prop}}\n\u003c/div\u003e\n```\n***\n### One-way Binding\nWatches only the identity of the parent property, not the property inside directive.\n```\nfunction MyDirective(){\n    var ddo = {\n        scope: {\n            prop: '\u003c'\n        }\n    };\n    return ddo;\n}\n```\n***\n##### _Summary_\n* To add functionality to the directive, one choice is to use a controller that's declared directly on the DDO.\n* Use controller property to declare controller in DDO.\n* Use bindToController and controllerAs props to bind declared properties in isolate scope directly to controller instance.\n* Define controller function as usual.\n* Whenever possible, use '\u003c' for one-way binding to save resources instead of bidirectional binding with '='.\n***\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Fcontrollers-inside-directives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnormandy72%2Fcontrollers-inside-directives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Fcontrollers-inside-directives/lists"}