SAP UI5和Angular里控制器(Controller)实现逻辑比较

简介: SAP UI5和Angular里控制器(Controller)实现逻辑比较

Let’s first refresh our memory on SAPUI5 controller. I have a simple xml view which only contains a button:


image.png

And a simple controller:

image.png


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:


image.png


And we can use JavaScript code in console to list the larget number of attributes belonging to created controller instance:


image.png


Or you can simply type “this.” in console, and see there are lots of methods available for controller instance:


image.png


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.


image.png


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:


image.png


Angular Controller

You can play with the sample Angular application from this url.


image.png


It consists of 31 lines of source code:

image.png


When you type a new language in input field and click “Add” button, the language will be added into list above:

image.png

Let me first briefly introduce the idea of source code, then I will go through with each point in detail.


image.png


(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.


image.png


Let’s have a look at the content of argument locals for factory function:


image.png


The most important attribute is $scope, which is passed into function NameCtrl defined by us:


image.png


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:


image.png


(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:


image.png

image.png

So it is called as expected:


image.png


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.


相关文章
|
1月前
|
前端开发 搜索推荐 开发者
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
SAP UI5 sap.m.Column 控件的 minScreenWidth 属性介绍
27 0
|
1月前
|
JavaScript 前端开发 开发者
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
SAP UI5 控件 sap.m.ListBase 的 inset 属性的作用介绍
13 0
|
1月前
|
Web App开发 数据采集 前端开发
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
纯技术讨论:如何让 SAP UI5 应用无法被别人在浏览器里调试 - 这种做法不推荐试读版
15 0
|
1月前
|
XML 存储 数据格式
SAP UI5 控件 customData 属性的应用介绍
SAP UI5 控件 customData 属性的应用介绍
31 0
|
1月前
|
前端开发 JavaScript API
SAP UI5 sap.ui.require.toUrl 的作用介绍
SAP UI5 sap.ui.require.toUrl 的作用介绍
28 0
|
1月前
|
搜索推荐
如何让 SAP UI5 Smart Table 支持多项选择(Multiple-Selection)试读版
如何让 SAP UI5 Smart Table 支持多项选择(Multiple-Selection)试读版
18 0
|
1月前
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
使用 SAP UI5 Event Bus 机制,修复 SAP UI5 分页显示数据的一个 bug 试读版
20 0
|
2月前
|
Web App开发 前端开发 JavaScript
乱花渐欲迷人眼 - 让 SAP UI5 应用的日志输出不再素面朝天
乱花渐欲迷人眼 - 让 SAP UI5 应用的日志输出不再素面朝天
46 0
|
2月前
|
Web App开发 JSON JavaScript
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
SAP UI5 应用程序小技巧 - 一键将 JSON 对象导出成本地 json 文件
25 0
|
2月前
|
XML 数据处理 数据格式
什么是 SAP UI5 XML Templating 技术
什么是 SAP UI5 XML Templating 技术
43 0