Angularjs Concept
Angularjs Note
- usage of
select options
Angular support the usage of select and options
. we can see the official document select, ngOptions .
Note: Matching model and option values
if the value
attribute of the option is specified, it will be treated as string. that means, the model should also be string, otherwise, miss matching will occur.
if the model
is a object or other non-string , we can use these ways:
. the ngOptions directive
. the ngValue directive, which allows arbitrary expressions to be option values (Example)
. model $parsers / $formatters to convert the string value (Example)
For the Sample code refer link1, link2.
- Services
we should make clear about the service
and services
, the first is an instance of the second. Now, we can see the definition of services
: we can use services to orginaze and share your code accross your app.
.
refer document of Services.
Key points:
. Lazy instantiated.
. Singletons.
How to use the services
, two steps: 1. register or definition 2. DI . For step1, we can call factory
or service
function to register or define the service.
the difference for factory
and service
is, in factory, only things under return
will be validate, for service
, we don't need return, this is the all content.
we can register a service over a module by calling factory or service function, or we can register it in the config function of a module by calling factory or service function over $prvider injected into the config function.
Detail, we can refer the link here.
we have a lot of build-in services like: $http, $q....
- Controllers
The Controller is used to argument the scope by the js contructor function. when a controller is instanced, a new child scope will be created and injected into the contructor of the controller by the parameter $scope,
if we specify the controllerAs property, the instance of controller will be assign to the $scope through the name of controllerAs, if not we can call $ctrl directly.
official document here.
Best pratise for usage of controller,
. Set up the initial state of the $scope object.
. Add behavior to the $scope object.
Document of ngController
- Components and Directives
Component Doc Directive Doc
Generally to say, component is a special kind of directive, which use simpler configuration.
for definition of component, just call component function with two params: componnetName,configuration object. for directive, we need to call the facotory function to return the configuration object.
For the difference of component and directive, we can refer the table in the above link.
Best Pratice for Component base architecture.
. Components only control their own View and Data
. Components have a well-defined public API - Inputs and Outputs : one-way or two-ways binding, =,<,&
. Components have a well-defined lifecycle:$onInit()...
.An application is a tree of components
Directives
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构