Skip to content

Instantly share code, notes, and snippets.

@higuma
Last active August 29, 2015 14:05
Show Gist options
  • Save higuma/65375a12aabb02063f04 to your computer and use it in GitHub Desktop.
Save higuma/65375a12aabb02063f04 to your computer and use it in GitHub Desktop.
doctype
html
head
meta(charset="UTF-8")
script(src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js")
body
div(ng-app,ng-init="qty=1;cost=2")
b Invoice:
div Quantity:
input(type="number", ng-model="qty")
div Costs:
input(type="number", ng-model="cost")
div
b Total:
| {{qty * cost | currency}}
<div ng-app="invoice1" ng-controller="InvoiceController as invoice">
angular.callbacks._0({
"query": {
...
"results": {
"rate": [
{ "id": "USDUSD", "Rate": "1.00", ... },
{ "id": "USDEUR", "Rate": "0.7467", ... },
...
] } }
});
var myModule = angular.module('myModule', []);
// Angular組み込みの$windowサービスをinject
var myController = function($window) { // パラメータ名($window)の変更は禁止!
this.greet = function(text) { $window.alert(text); };
};
myModule.controller('myController', myController);
var myModule = angular.module('myModule', []);
var myController = function(w) { // パラメータ名は任意でよい
this.greet = function(text) { w.alert(text); };
};
myController.$inject = ['$window']; // ここに記述する
myModule.controller('myController', myController);
var myModule = angular.module('myModule', []);
var myController = function(w) {
this.greet = function(text) { w.alert(text); };
};
myModule.controller('myController', ['$window', myController]);
angular.module('myModule', [])
.controller('myController', [
'$window',
function(w) {
this.greet = function(text) { w.alert(text); };
}
]);
<button ng-click="greet()">Greet</button>
<button ng-click="window.alert('Should not see me')">Won't greet</button>
angular.module('expressionExample', [])
.controller('ExampleController', [
'$window', '$scope',
function($window, $scope) {
$scope.name = 'World';
$scope.greet = function() { $window.alert("Hello " + $scope.name); };
}
]);
angular.module(...)
.filter('フィルタ名', function() {
function(入力, もしあれば引数) {
// 戻り値がフィルタ出力
}
})
.controller(...以下略...)
angular.module(モジュール名, [依存モジュール, ...])
テンプレート <=> スコープ <=> コントローラ
<div ng-app="モジュール名" ng-controller="コントローラ名 as 変数名">
テンプレート スコープ コントローラ
+----------------+ +--+ +----------------------------------+
| username <---->|<-->|..|<-->|<--> $scope.username (モデル) |
| | | | | |
| sayHello() ----|--->|..|--->|---> $scope.sayHello (メソッド) |
| | | | | |
| {{greeting}} <-|<---|..|<---|---- $scope.greeting (プロパティ) |
+----------------+ +--+ +----------------------------------+
.css-form input.ng-invalid.ng-dirty {
background-color: #fa787e; /* red */
}
.css-form input.ng-valid.ng-dirty {
background-color: #78fa89; /* green */
}
<a ng-href="img/{{username}}.jpg">Hello {{username}}!</a>
<svg>
<circle cx="{{cx}}" cy="{{cy}}" r="{{r}}"></circle>
</svg>
<svg>
<circle ng-attr-cx="{{cx}}" ng-attr-cy="{{cy}}" ng-attr-r="{{r}}"></circle>
</svg>
angular.module('アプリケーションモジュール名', [...])
.config(['$locationProvider, function($locationProvider) {
$locationProvider // 設定項目は次の2つだけ
.html5Mode(true) // false(デフォルト)はhashbangモード
.hashPrefix('!'); // #!...を使う場合の設定例(デフォルトは'')
}])
var oldXHR = XHR; // XHRを保存
XHR = function MockXHR() {}; // モックに差し替え(説明用:本当はJasmineのSpyなどを使う)
var myClass = new MyClass(); // このMyClassがテストの対象
myClass.doWork(); // doWorkメソッドの中でnew XHR()を使う
// MockXHRが正しい引数でメソッドコールされていたかアサーション実行(略)
XHR = oldXHR; // 元に戻す(これを忘れると何か悪いことが起きる?)
// 一回このようなものを作っておけば汎用ユーティリティとして使える
function mockTest(globalObject, property, mockObject, testfunc) {
var originalProperty = globalObject[property];
globalObject[property] = mockObject;
// try/finallyを使えばたとえ例外が発生しても確実に復元できる
try { testFunc.call(); } // テスト実行
finally { globalObject[property] = originalProperty; } // 元に戻す
}
// 利用方法(何だかDIに似てきた?)
mockTest(window, 'XHR', function MockXHR() {}, function() {
var myClass = new MyClass();
myClass.doWork();
// (略)
});
angular.module('モジュール名', [ /*依存モジュール(今回はなし)*/ ])
.controller('コントローラ名', function() {
// コンストラクタ関数のコード
// * thisは生成中のコントローラインスタンス
// * コントロール要素(input等)とやり取りするデータはthisのプロパティ
// * メソッドもthisのプロパティ
...
// 終了時に戻り値付きでreturnしないこと(return thisなら可)
})
angular.module('invoice1', [])
.controller 'InvoiceController', ->
@qty = 1
@cost = 2
@inCurr = 'EUR'
@currencies = ['USD', 'EUR', 'CNY', 'JPY']
@usdToForeignRates =
USD: 1
EUR: 0.74
CNY: 6.09
JPY: 102.6
@total = (outCurr) -> @convertCurrency @qty * @cost, @inCurr, outCurr
@convertCurrency = (amount, inCurr, outCurr) ->
amount * @usdToForeignRates[outCurr] / @usdToForeignRates[inCurr]
@pay = -> window.alert "Thanks!"
@ # <= 注意! これを忘れると動作しない
テンプレート <=(スコープ)=> コントローラ(invoice2) <=> サービス(finance2)
angular.module('モジュール名', ['外部依存モジュール名'])
.controller('コントローラ名', [
'利用するサービス名(ファクトリーID)',
function(生成したサービスのインスタンス) {
// コントローラのコンストラクタコード
}
]);
angular.module('モジュール名', ['サービス1', 'サービス2'])
.controller('コントローラ名', [
'ファクトリーID1', 'ファクトリーID2',
function(サービス1のインスタンス, サービス2のインスタンス) {
// コントローラのコンストラクタコード
}
]);
angular.module('モジュール名', [/*もしあれば外部依存モジュール*/])
.factory('ファクトリーID', function() {
// サービスのコンストラクタコード
// ...
// オブジェクトリテラルを返す
return {
プロパティ: 値または関数,
...
};
});
angular.module('finance3', [/*($httpはAngular組み込みなのでここは空)*/])
.factory('currencyConverter', [
'$http', // $httpサービスを利用
function($http) { // やはりinjectionを使う
// この中に$httpインスタンスを「注入」して利用する
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment