{"id":15184722,"url":"https://github.com/cnlon/angular-controller","last_synced_at":"2026-03-04T10:01:15.650Z","repository":{"id":57178217,"uuid":"101179353","full_name":"cnlon/angular-controller","owner":"cnlon","description":"AngularController 用于改进 Angular1 编码体验，开启 ESNext 使用新模式。","archived":false,"fork":false,"pushed_at":"2019-03-21T06:51:48.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-26T16:59:43.537Z","etag":null,"topics":["anguarljs","angular","angular1","async","es6","promise"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cnlon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-23T12:49:14.000Z","updated_at":"2020-06-08T11:10:03.000Z","dependencies_parsed_at":"2022-09-09T17:12:02.631Z","dependency_job_id":null,"html_url":"https://github.com/cnlon/angular-controller","commit_stats":null,"previous_names":["cnlon/angular-scope-controller"],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/cnlon/angular-controller","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cnlon%2Fangular-controller","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cnlon%2Fangular-controller/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cnlon%2Fangular-controller/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cnlon%2Fangular-controller/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cnlon","download_url":"https://codeload.github.com/cnlon/angular-controller/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cnlon%2Fangular-controller/sbom","scorecard":{"id":294699,"data":{"date":"2025-08-11","repo":{"name":"github.com/cnlon/angular-controller","commit":"8c1fe974ccdb56d9e45f96410f763b62a4655ab8"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-17T19:10:46.550Z","repository_id":57178217,"created_at":"2025-08-17T19:10:46.551Z","updated_at":"2025-08-17T19:10:46.551Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30078306,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T08:01:56.766Z","status":"ssl_error","status_checked_at":"2026-03-04T08:00:42.919Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["anguarljs","angular","angular1","async","es6","promise"],"created_at":"2024-09-27T17:22:43.680Z","updated_at":"2026-03-04T10:01:15.625Z","avatar_url":"https://github.com/cnlon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# angular-controller\n\n[![npm version](https://badge.fury.io/js/angular-controller.svg)](https://badge.fury.io/js/angular-controller)\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com)\n\nAngularController 用于改进 Angular1 编码体验，开启 ESNext 使用新模式。\n\n快速预览（[地址](https://stackblitz.com/edit/angular-controller)）：\n\n```javascript\nimport angular from 'angular'\nimport { AngularController, inject, watch } from 'angular-controller'\n\n@inject('$scope')\nclass AppController extends AngularController {\n  static NAME = 'AppController'\n\n  $onState() {\n    return {\n      value: 'angular controller',\n      upperCaseValue: ''\n    }\n  }\n\n  @watch('value', { immediate: true })\n  onValueChanged(value) {\n    this.upperCaseValue = value \u0026\u0026 value.toUpperCase()\n  }\n}\n\nangular.module('App', []).controller(AppController.NAME, AppController)\n```\n\n```html\n\u003cdiv ng-app=\"App\"\u003e\n  \u003cdiv ng-controller=\"AppController\"\u003e\n    \u003cinput ng-model=\"value\"/\u003e\n    \u003cspan\u003e{{upperCaseValue}}\u003c/span\u003e\n  \u003c/div\u003e\n\u003c/div\u003e\n```\n\n## 为什么？\n\n1. Angular1 的 Controller 无法使用 function prototype 和 ES6 class 特性，原因在其模板作用域（`$scope`）并未使用 Controller 实例（`this`）\n2. 虽然 1.2 版本后可以通过设置 `controllerAs` 补救，但这样却又破坏了 HTML 模板原有语法，模板中所有原来与 $scope 绑定的属性、方法前都需要加上前缀，使用体验仍然不佳\n3. 此外在 1.4 版本之前不支持 `bindToController`，使用 `controllerAs` 选项后，指令的继承属性仍然必须从 `$scope` 而非实例上获取\n\nAngularController 解决了以上痛点，并提供了大量的优化特性，包括：\n\n1. 声明式属性\n2. 实例方法或 getter/setter 自动绑定 this\n3. 支持继承\n4. 实例上提供 $scope 上常用 API，如：`$watch`, `$on`, `$emit`, `$broadcast`\n5. 统一 `$watch`，`$watchCollection`，`$watchGroup` 三个方法，初始化时不再默认执行\n6. 支持计算属性（Computed Properties）\n7. 提供多个装饰器，如：watch，on，emit，broadcast 和 emitBefore，broadcastBefore\n8. 推荐 async/await 语法（需配置）\n9. ...\n\n## 安装\n\n```shell\nnpm install angular-controller\n```\n\n注意：angular-controller 默认使用 ESNext 语法，如需兼容 ES5，请自行编译\n\n## 快速上手\n\nAngularController 默认使用 ESNext 新特性，包括：module、class、decorator、async/await 等。如果你对 ESNext 新特性还不熟悉，建议先[预习](http://es6.ruanyifeng.com/)一下再来。\n\n首先来看一个精简过的示例：\n\n```javascript\n@inject('$scope')\nclass Controller extends AngularController {\n  $onState () {\n      return {\n          name: 'world'\n      }\n  }\n  sayHello () {\n      return 'Hello ' + this.name + '!'\n  }\n}\n```\n\n上例中使用 AngularController 所需步骤有：\n\n1. 创建 Controller 类，继承 `AngularController`\n2. 使用装饰器 `@inject` 注入所需依赖\n3. 创建实例方法 `$onState` 声明属性\n4. 创建自定义实例方法\n\n其中：\n\n**1. 基类 `AngularController`**\n\n使用 AngularController 必须继承基类 `AngularController`。基类 `AngularController` 在创建之初会执行 `$onState` 方法得到初始属性，并将属性和所有可用实例方法代理至 `$scope`；此外，`AngularController` 还代理和提供了多个内置实例 API，包括：`$set`、`$on`、`$emit`、`$broadcast`和`$watch` 方法，这些方法后面会逐个介绍。\n\n**2. 装饰器 `@inject`**\n\n装饰器 `@inject` 用于注入依赖，一般必须要注入 `$scope`。注入名称无先后顺序，注入后所有服务会挂载在实例下，可以通过类似 `this.$scope` 的方式来访问。\n\n**注意：** 默认支持的最新的[装饰器提案](https://github.com/tc39/proposal-decorators)，如果需支持旧的提案，修改入口为 `'angular-controller/lagacy/'` 即可\n\n**3. 声明属性 `$onState`**\n\n`$onState` 方法用于声明属性，所有需要参与脏检查机制的属性，即在模板中可以获取的属性、需要被 watch 的属性以及父组件传递下来的接口属性等都需要在此声明，这些属性将会绑定至实例，并代理至 `$scope`；声明时可以设置默认值，如不希望设置默认值，可以设置值为 `undefined`；使用时直接通过类似 `this.name` 方式来读取和通过类似 `this.name = 'AngularController'` 的方式来改变属性，改变将会实时反映至 `$scope` 上。\n\n**4. 实例方法和实例 getter/setter**\n\n所有实例方法或实例 getter/setter（除 constructor、以 '$' 开头的和不可配置（configurable 为 true）的以外），在创建 Controller 时会和属性一并代理至 `$scope`；代理至 `$scope` 的方法会绑定实例，所以无需担心内部 `this` 指向问题。\n\n## API\n\n#### 基类 AngularController（待完善）\n\n实例方法\n\n- $onState\n- $onInit\n- $set\n- $watch\n- $on\n- $emit\n- $broadcast\n\n装饰器\n\n- inject\n- watch\n- on\n- emit\n- emitBefore\n- broadcast\n- broadcastBefore\n\n#### 辅助函数 overwritePromise，overwriteBabelRuntimePromise，overwriteBrowserAndBabelRuntimePromise\n\n应用启动时覆盖 Promise 为 $q，使得可以直接使用 Promise，async/await\n\noverwritePromise 用于覆盖全局 Promise\n\n示例：\n\n```javascript\nimport angular from 'angular'\nimport overwritePromise from 'angular-controller/overwritePromise.js'\n\n// 根应用启动时\nangular.module('YourApp').controller('YourMainController', ['$q', function ($q) {\n  overwritePromise($q)\n  // 之后整个项目就可直接使用 Promise 和 async/await 了\n}])\n```\n\noverwriteBabelRuntimePromise 用于覆盖 babel-plugin-transform-runtime 生成的 Promise， overwriteBrowserAndBabelRuntimePromise 两者都会覆盖。用法一致，不作说明。\n\n***\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcnlon%2Fangular-controller","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcnlon%2Fangular-controller","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcnlon%2Fangular-controller/lists"}