Home > php教程 > PHP开发 > body text

AngularJS operates key-value objects similar to java's hashmap

高洛峰
Release: 2016-12-07 10:05:39
Original
1479 people have browsed it

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;
}
};
})
Copy after login

"[]" 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];
}
};
})
Copy after login

4. Verify

1. Write

ParamsServices.put("itv", "itv");
ParamsServices.put("itv2", "itv2");
iParamsServices.put("itv3", "itv3");
iParamsServices.put("itv4", "itv4");
Copy after login

2. Read

ParamsServices.get("itv") == ParamsServices.get("itv2") // true
iParamsServices.get("itv3") == iParamsServices.get("itv4") // false
Copy after login

5. Summary:

angul as key value object ( hashmap) recommended method Second, realize


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template