Let’s first refresh our memory on SAPUI5 controller. I have a simple xml view which only contains a button:
<core:View xmlns:core="sap.ui.core" xmlns:common="sap.ui.commons" controllerName="buttontutorial.view.simple">
<common:Button text="Jerry" id="jerryButton"/>
</core:View>
And a simple controller:
sap.ui.define(["sap/ui/core/mvc/Controller"], function(Controller){
"use strict";
return Controller.extend("buttontutorial.view.simple",{
onInit : function() {
debugger;
}
});}
);
due to the attribute controllerName=”buttontutorial.view.simple” in XML view, the controller instance is created and connect with XML view instance by UI5 framework:
And we can use JavaScript code in console to list the larget number of attributes belonging to created controller instance:
for( var name in this ) { console.log("attribute: " + name + " value: " + this[name]);}
Or you can simply type “this.” in console, and see there are lots of methods available for controller instance:
For example, byId method of controller instance is widely used, if you type this.byId in console, you can see its implementation just delegates the call to this.oView.byId.
This makes sense since every controller instance holds a reference to its host view via oView, and the connection between controller and its view is established in function connectToView:
Angular Controller
You can play with the sample Angular application from this url.
It consists of 31 lines of source code:
<html ng-app>
<head>
<meta charset="utf-8">
<title>Angular.js Example</title>
<script src="angular/angular.js"></script>
<script>
function NameCtrl($scope){
$scope.names = ['ABAP', 'Java'];
$scope.addName = function() {
if( $scope.names.indexOf($scope.enteredName) != -1){
alert("duplicate key is not allowed in list: " + $scope.enteredName);
$scope.enteredName = '';
return;
}
$scope.names.push($scope.enteredName);
$scope.enteredName = '';
};
}
</script>
</head>
<body ng-controller="NameCtrl">
<ul>
<li ng-repeat="name in names">{{name}}
</li>
</ul>
<form ng-submit="addName()">
<input type="text" ng-model="enteredName">
<input type="submit" value="add">
</form>
</body>
</html>
When you type a new language in input field and click “Add” button, the language will be added into list above:
Let me first briefly introduce the idea of source code, then I will go through with each point in detail.
(1) controller instance initialization
During Angular bootstrap phase, due to this line of source code in html, , Angular will create a new controller instance in line 5327. You can consider $controller as a factory function.
Let’s have a look at the content of argument locals for factory function:
The most important attribute is $scope, which is passed into function NameCtrl defined by us:
Once our application code is executed, controller instance is created. However, after checking it in Chrome, I found it is just a dummy instance without any important attribute. Instead, the data model and addName function are appended and available in current scope:
(2) How addFunction available in scope object will be called when add button is called
Based on study result in step1, the addName function is located in scope object. My question is, when I press add button, why is it called?
In previous blog Compare Event handling mechanism: SAPUI5 and Angular, I already introduced that Angular does event handler registration automatically for us, as long as it detects the directive like this:
.
Actually I have made modifications on angular.js, adding more trace so that you can easily find where Angular achieves event registration under the hood:
So it is called as expected:
Summary
UI5 controller instance has a large number of useful functions available to use, and for Angular, controller instance is just a dummy one: data model and event handler function are located in scope object instead.
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
5. `My97DatePicker`:这可能是一个日期选择器插件,集成到WebUI4Angular中,提供日期输入和选择功能。 6. `css`:样式表文件夹,包含了WebUI4Angular的CSS样式文件,定义了组件的外观和布局。 7. `misc`:杂项文件...
在Angular开发中,异步加载Controller是一种常见的优化技术,它能显著提高应用的启动速度和性能,特别是对于大型单页应用程序(SPA)。这种方式允许我们按需加载模块和控制器,而不是一次性加载整个应用,从而减少...
在移动应用开发中,AngularJS是一个强大的MVC(模型-视图-控制器)JavaScript框架,由Google维护,它极大地简化了前端开发,尤其是数据绑定和依赖注入方面。AngularJS通过声明式编程让开发者能更专注于业务逻辑,而...
5. **可定制性**:Angular UI Bootstrap 允许开发者通过 CSS 类或配置对象来自定义组件外观和行为。 6. **持续更新**:项目维护活跃,随着 Bootstrap 的新版本发布,Angular UI Bootstrap 也会及时更新,以保持与...
Mobile Angular UI 是个 HTML5 移动端 UI 框架,允许使用 Angular JS 和 Bootstrap 3 来开发移动端应用。 Mobile Angular UI 在 Bootstrap 3 的基础上新增了: switches, overlays, sidebars, scrollable areas, ...
在Angular.js框架中,控制器(Controller)之间默认不共享Scope,这意味着如果要在不同的Controller之间传递数据,我们需要采取一些特殊的方法。以下将详细介绍两种常见的Angular.js跨Controller实现参数传递的方法...
1. MVC架构:SAPUI5基于MVC模式,将应用程序逻辑分为模型(Model)、视图(View)和控制器(Controller)三个部分。模型负责处理数据,视图用于显示用户界面,控制器则协调两者之间的交互。 2. OData支持:OData是...
接着,你可以在控制器、指令或其他服务中使用它来启动和停止UI的阻塞。 **启动和停止阻塞** 在Angular Block UI中,你可以使用`blockUI.start()`和`blockUI.stop()`方法来控制UI的阻塞状态。通常,这些方法会在...
通过定义`controller`、`template`和`view`等属性,UI-Router能够精确控制特定位置的用户界面和交互行为。 #### 二、状态管理 在UI-Router中,**状态**是核心概念之一。每个状态对应于应用的一个特定部分或“页面...
总结,"Angular Service Controller分层示例"强调了如何利用AngularJS的服务和控制器来构建可维护的前端应用,并通过与后端的Spring MVC集成,实现完整的MVC架构。理解并熟练运用这些概念对于开发高质量的Web应用至...
WebUI4Angular是基于angularjs实现的一套UI组件,主要包括:datagrid,tree,dialog,progress,tilelist,tabset,dividedbox,imageview等等。组件需要使用angularjs1.3以上版本,其中拍照组件cameraScanner另需安装flash...
而`angular-ui-sortable`是AngularJS的一个扩展模块,它将jQuery UI的Sortable功能集成到了AngularJS中,使得在Angular应用中实现元素的拖拽排序变得简单易行。 `angular-ui-sortable`指令允许开发者在AngularJS...
AngularJS是一款强大的前端JavaScript框架,...通过学习这个示例,初学者可以理解如何设置和使用UI-Router,从而在AngularJS项目中实现更复杂的导航和视图管理。这将有助于提高他们构建动态和交互性强的Web应用的能力。
3. **视图(Views)**:视图是UI Router用来展示状态内容的容器,可以在一个状态下定义多个视图,每个视图可以有自己的模板和控制器。 4. **嵌套路由(Nested States & Views)**:UI Router支持嵌套路由,允许在一...
Angular UI Grid 是一个强大的数据网格组件,专为 AngularJS 应用程序设计,用于展示和操作大量结构化的数据。它提供了一系列高级功能,如分页、排序、过滤、行选择、列固定、自定义编辑器和渲染器等,帮助开发者...
5. **复用验证规则**:由于验证逻辑定义在控制器的函数中,因此可以方便地在整个应用中复用这些规则,提高代码的可重用性。 在实际开发中,Angular UI Validate特别适合处理那些标准验证规则无法满足的场景,比如...
本文将详细介绍两种在Angular中实现控制器之间传递参数的方法。 1. **通过$rootScope传参** AngularJS的作用域采用原型链机制,因此在子作用域中无法直接访问的属性,会向上级作用域查找。$rootScope作为所有控制...
同时,我们还使用了Angular的内置服务,例如$rootScope和$timeout来实现应用程序的逻辑和交互。 Angular之toDoList的实现代码示例提供了一个基本的toDoList应用程序的实现思路和方法,为开发者提供了一个良好的参考...
Angle-3.4-mvc5-angular 是一个项目,它整合了多个关键的技术栈,包括Angle、MVC5、Angular以及AngularJS,旨在提供一个高效、可扩展的前端和后端解决方案。这个压缩包包含了两个主要的子项目:backend-mvc5-angular...
你需要将所需的JavaScript和CSS文件引入到你的项目中,然后通过AngularJS的依赖注入机制加载ui.calendar模块,接着就可以在你的控制器或指令中使用calendarConfig服务进行配置,以定义日历的行为和外观。 在实际...