Foreword:
We know that the most commonly used hashmap methods in Java are put(...), get(...) and remove() methods, so how to create (use) such an object in angularJS
Thinking Analysis:
We know that in Java, chain access and "[]" can be used to access a certain value of hashmap
Specific implementation:
Chain access:
.factory('ParamsServices', function () { var params = {}; return { get: function (key) { return params.key; }, put: function (key, object) { params.key = object; }, remove: function (key) { delete params.key; } }; })
"[]" access:
.factory('iParamsServices', function () { var map = {}; return { get: function (key) { return map[key]; }, put: function (key, object) { map[key] = object; }, remove: function (key) { delete map[key]; } }; })
4. Verify
1. Write
ParamsServices.put("itv", "itv"); ParamsServices.put("itv2", "itv2"); iParamsServices.put("itv3", "itv3"); iParamsServices.put("itv4", "itv4");
2. Read
ParamsServices.get("itv") == ParamsServices.get("itv2") // true iParamsServices.get("itv3") == iParamsServices.get("itv4") // false
5. Summary:
angul as key value object ( hashmap) recommended method Second, realize