2018年6月5日 星期二

[JS/Ang]鍵盤偵聽事件、event filter、TS中函式參數的宣告

输入框事件监听(一):keydown、keyup、input -blog.csdn.net/yiifaa 甘焕的博客

這是在學ng時碰到的作業,
直接在 html塞 on***="",以前都沒這樣寫過,為時未晚。

例如:在圖片塞 onclick,點擊後讓自己或其他元件產生變化;
在 input塞 keydown等等。

Ng的事件過濾器 event filter
(keyup)改為 (keyup.escape)或是 (keyup.enter) 這兩個常用的鍵;
搭配使用 $event.target / $event.target.value 。

在 component寫函式時,
寫入參數同時加入型別或屬性(property)有助於程式碼撰寫及除錯,
也可增加維護或修改效率,
例如:
countText(count: string) {
this.number = count.length;
}

原本未加入string,如果輸入 count.lenth,vs code不會有錯誤提示,
加入string後會出現提示。

countTextClean(input: HTMLInputElement) {
...
}

宣告之後可以在輸入‘.’時用選的,減少出錯。
也有看到有些寫法會使用any: (parameter : any)

[CSS]Sass中的HSL function

Sass HSL function -wcc723.github.io,討論顏色處理的function

常用的darken & lighten (color, n%),
還有飽和 Saturate & Desaturate(color, n%);

lightness(color)取得顏色的明度,就像 selector.text 的取得。

hue & adjust-hue(color, n deg),
前者取得色環位置,後者取得調整後呈現;

grayscale(color) 調整為灰階,這裏調整後明度不變;

complement & invert (color),互補色(負片)及反相色,
這兩個差異我也不清楚,應是跟RGB三原色、CMYK二次色有關係。
ps調色前必知的色彩原理 -愛經驗

[JS]將函數當成參數

我們知道函數可以寫成這樣:

function functionName (n, m){ ... }

也可以

function functionName (funcName, m){
...
return funcName (r)
}

2018年6月1日 星期五

[HTML]使用自定義屬性、並能通過 html5驗證:data-*

趕案子的時候因為時間緊迫,
常常直接使用 plan-U(ugly) 而割愛捨棄 plan-A~C,
導致維護麻煩,所以要常常祈求不需維護

例如下面這個 plan-U程式碼,當使用者沒有選取任何物件,
或選擇數量不同的物件時,img 會跟著抽換 :



















這樣子又醜又胖,且 attr寫死在js裏。

於是結案後回想這個問題,想到可以寫函式將他們包住,
富經驗的前端高高手朋友更建議可以將 img圖檔直接存入 html。
什麼意思? 使用data-*
HTML 5 自訂屬性 -Inet@Web

可以在 <img>這個 tag裏新增自定義的屬性,
但隨意命名不會通過 html5的驗證,例如:
<img src='...' layer01='diamondDarkest.png' alt='...'>
這樣可以使用,但不會通過驗證。
改成 data-layer01就可以囉!
這樣一來也更加符合資料呈現放在html的原則,
接著將 attr()寫成 attr('src','data-layer01') 就更方便也更整齊囉!

依上方文章推測,也許還能這樣寫,類似物件導向的方式:
attr('src', dataset.layer01) 
最後這個寫法純屬臆測,尚未證實。

所以圖片的 .topImg img.txt2, .topImg img.txt3 原本html img tag中,
加入data-layer01 ~ data-layer04,後包入函示並給參數來調整src、display的內容,
就能解決讓程式碼更易讀、利於維護囉。

2018年5月31日 星期四

[JS]要delay display的時候用到的 queue()

若要讓 display從 none到 unset,
需用到 queue()
http://www.w3school.com.cn/jquery/data_jquery_queue.asp

最佳解及說明在這裡
https://stackoverflow.com/questions/5396119/using-jquery-delay-with-css
$('h2').delay(800).css('display','none')
會沒有作用
要改成
$('h2')
  .delay(800)
  .queue( function(next){
    $('this').css('display','none');
    next();
  });

而next()的作用在文中也有解釋,只是我還是不懂。

也有用setTimeOut的做法。

2018年5月30日 星期三

[Ang]繫結binding:內嵌、屬性、事件

內嵌 {{ }}
在 app.component.ts檔案中設定的值,
可以直接在 app.component.html (template)調用,
可讓在標籤之間,也可放在屬性中,
也可以寫入回呼建構式。

屬性 []
這裡的屬性是指 DOM的 property,非 Html的 attribute,
要查詢 property可在開發者工具裡的找到








打開第二項的HTMLImageElement,
可以看到有 alt, attribute, title ...
eg: href="{{ url }}"
改寫成 [href]="url"。
[ ]綁定只限property,例如<a [myData]= "..."></a>會產生語法錯誤,
因為myData並不是property。
在ng中自訂字如要綁定,則使用:
[attr.自訂字]= " ..."
在[ ]自訂自前方加入attr.即可。

事件 ()
事實上,ng裡面並沒有function,
只有類別,類別裡面只有屬性跟方法。
在標籤中將原本 onclick=""改為 on-click=""就變成ng的語法,
然後放入方法,例如:
<img on-click="change()"></img>
其中change(),來自component。
而比較建議的做法是
把 on移除,加上 ()改為 (click)="",可讀性也高。
在事件 binding使用上," "中按下
win ctrl+space
mac
會跳出建議選項,再自行加入(),方便使用。

事件參數 ($event)
可以做到控制有無按下option(alt)+click,發生事件。
但要注意大小寫,例如 $event.altKey ;
我們在 component中加入 console.log($event);
進入開發工具來觀察,型別是MouseEvent。
展開可以看到有非常多 property能使用,
為避免寫錯大小寫,我們可以在參數後方加入型別,例如
functionName($event: MouseEvent) { ... }
這樣在 $event後輸入 ' . ' 就可以用選的,用tab鍵自動輸入,降低打錯字的機率。

2018年5月29日 星期二

[Ang]部署的方法、升級 angular應用程式到新版本的方法

ng build
後會建立一個dist folder,
裡面若有不需要的檔案,到 angular.json中"assets"找到檔案名稱並刪除。

刪除後改執行
ng build --prod
product的意思,會自動幫我們合併壓縮檔案,
表示網頁載入效率及速度更好。

若我們在查詢版本時
ng -v
出現下列類似字樣:




那麼我們可進行更新動作
ng update
可在 git觀察 package.json升級前後的版本差異

檢查 global angular cli的方法
npm list -g --depth=0
or
yarn global list --depth=0
depth=0表示第一層。即可以查詢到目前版本,
接著輸入
npm outdated -g
or
yarn outdated
沒有顯示任何東西表示已是最新版本,
若有就輸入安裝指令即可。

2018年5月28日 星期一

[Ang]快速建立元件、vs code切換template、全部儲存、搜尋開啟檔案快速鍵、加入靜態網頁版型

ng g component pagename
其中 component可簡化為 c

會造出

四個檔案

想知道可以製造哪些類型,可鍵入ng g -h


快速鍵:https://defkey.com/visual-studio-code-mac-shortcuts

  • 快速跳到 template html,在template中鍵入則會跳到component
    win alt+o
    mac shift+option+o
  • 全部儲存
    win crtl+k+s
    mac option+cmd+s
  • 搜尋關鍵字開啟檔案,檔名甚至可以忽略小數點給參考項目
    win ctrl+e
    mac cmd+p(這個非常富功能性)
加入靜態網頁版型、圖片、CSS or JS:
放到src資料夾,再到專案目錄下 angular.json 
"app": [{ ... "assets":[ 修改 ] ... }] 
完畢後須重啟 
npm start / yarn start

2018年5月21日 星期一

[Ang]安裝環境:升級之前安裝過的node、安裝 angular cli

yarn的使用及升级Node.js的新方法 - haorooms.com

Angular 2 開發實戰:新手入門篇 實作環境說明 - 保哥github
Angular 开发学习 02 – Angular CLI - DEVBEAN TECH WORLD
install:
yarn global add @angular/cli
or
npm install -g @angular/cli

檢查
ng -v
但在這裡我出現error
Error: Cannot find module '@angular-devkit/core' - after clean install - stackoverflow















而且嘗試移除也失敗,只好暫時先不管。

保哥回覆:
請你試著用 npm 移除重裝看看。 
npm uninstall -g @angular/cli 
npm install -g @angular/cli 

開新資料夾後建立專案
ng new name
接著啟動
yarn start
就可以點 localhost:4200後自動從瀏覽器開啟檢查囉
Use Angular-CLI with Yarn - winsmarts.com
上面這篇改為 yarn沒有試成功
ng set --global packageManager=yarn

但是沒有出現 .angular-cli.json 檔案
How to generate .angular-cli.json file in Angular Cli? - stackoverflow
Missing .angular-cli.json file: Angular - stackoverflow
下面這篇有人回覆:
Update: With Angular 6.0.0 the filename is changed from .angular-cli.json to angular.json.
原來是改名字了且從 hidden 現形。

進入網頁後,右鍵檢視原始碼,(略不同於 option+cmd+I)
<base> 是非常重要的標籤!