Skip to content

Commit a249c58

Browse files
committed
add angularjs practice
- ng-repeat, filter usage - update study
1 parent 75c69d4 commit a249c58

27 files changed

Lines changed: 8797 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
logs/*
2+
!.gitkeep
3+
node_modules/
4+
app/lib/
5+
tmp
6+
.DS_Store
7+
.idea
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"strict": "global",
3+
"globals": {
4+
// AngularJS
5+
"angular": false,
6+
7+
// AngularJS mocks
8+
"module": false,
9+
"inject": false,
10+
11+
// Jasmine
12+
"jasmine": false,
13+
"describe": false,
14+
"beforeEach": false,
15+
"afterEach": false,
16+
"it": false,
17+
"expect": false,
18+
19+
// Protractor
20+
"browser": false,
21+
"element": false,
22+
"by": false
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
dist: trusty
2+
3+
language: node_js
4+
node_js:
5+
- 10
6+
7+
addons:
8+
chrome: stable
9+
10+
cache:
11+
directories:
12+
- "$HOME/.npm"
13+
14+
install:
15+
- npm install
16+
17+
script:
18+
- npm run test-single-run -- --browsers ChromeHeadless
19+
- (npm start > /dev/null &) && npm run protractor -- --capabilities.chromeOptions.args headless
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright (c) 2010-2016 Google, Inc. http://angularjs.org
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
# `angular-seed` — the seed for AngularJS apps
2+
3+
This project is an application skeleton for a typical [AngularJS][angularjs] web app. You can use it
4+
to quickly bootstrap your angular webapp projects and dev environment for these projects.
5+
6+
The seed contains a sample AngularJS application and is preconfigured to install the AngularJS
7+
framework and a bunch of development and testing tools for instant web development gratification.
8+
9+
The seed app doesn't do much, just shows how to wire two controllers and views together.
10+
11+
12+
## Getting Started
13+
14+
To get you started you can simply clone the `angular-seed` repository and install the dependencies:
15+
16+
### Prerequisites
17+
18+
You need git to clone the `angular-seed` repository. You can get git from [here][git].
19+
20+
We also use a number of Node.js tools to initialize and test `angular-seed`. You must have Node.js
21+
and its package manager (npm) installed. You can get them from [here][node].
22+
23+
### Clone `angular-seed`
24+
25+
Clone the `angular-seed` repository using git:
26+
27+
```
28+
git clone https://github.com/angular/angular-seed.git
29+
cd angular-seed
30+
```
31+
32+
If you just want to start a new project without the `angular-seed` commit history then you can do:
33+
34+
```
35+
git clone --depth=1 https://github.com/angular/angular-seed.git <your-project-name>
36+
```
37+
38+
The `depth=1` tells git to only pull down one commit worth of historical data.
39+
40+
### Install Dependencies
41+
42+
We have two kinds of dependencies in this project: tools and AngularJS framework code. The tools
43+
help us manage and test the application.
44+
45+
* We get the tools we depend upon and the AngularJS code via `npm`, the [Node package manager][npm].
46+
* In order to run the end-to-end tests, you will also need to have the
47+
[Java Development Kit (JDK)][jdk] installed on your machine. Check out the section on
48+
[end-to-end testing](#e2e-testing) for more info.
49+
50+
We have preconfigured `npm` to automatically copy the downloaded AngularJS files to `app/lib` so we
51+
can simply do:
52+
53+
```
54+
npm install
55+
```
56+
57+
Behind the scenes this will also call `npm run copy-libs`, which copies the AngularJS files and
58+
other front end dependencies. After that, you should find out that you have two new directories in
59+
your project.
60+
61+
* `node_modules` - contains the npm packages for the tools we need
62+
* `app/lib` - contains the AngularJS framework files and other front end dependencies
63+
64+
*Note copying the AngularJS files from `node_modules` to `app/lib` makes it easier to serve the
65+
files by a web server.*
66+
67+
### Run the Application
68+
69+
We have preconfigured the project with a simple development web server. The simplest way to start
70+
this server is:
71+
72+
```
73+
npm start
74+
```
75+
76+
Now browse to the app at [`localhost:8000/index.html`][local-app-url].
77+
78+
79+
## Directory Layout
80+
81+
```
82+
app/ --> all of the source files for the application
83+
app.css --> default stylesheet
84+
core/ --> all app specific modules
85+
version/ --> version related components
86+
version.js --> version module declaration and basic "version" value service
87+
version_test.js --> "version" value service tests
88+
version-directive.js --> custom directive that returns the current app version
89+
version-directive_test.js --> version directive tests
90+
interpolate-filter.js --> custom interpolation filter
91+
interpolate-filter_test.js --> interpolate filter tests
92+
view1/ --> the view1 view template and logic
93+
view1.html --> the partial template
94+
view1.js --> the controller logic
95+
view1_test.js --> tests of the controller
96+
view2/ --> the view2 view template and logic
97+
view2.html --> the partial template
98+
view2.js --> the controller logic
99+
view2_test.js --> tests of the controller
100+
app.js --> main application module
101+
index.html --> app layout file (the main html template file of the app)
102+
index-async.html --> just like index.html, but loads js files asynchronously
103+
e2e-tests/ --> end-to-end tests
104+
protractor-conf.js --> Protractor config file
105+
scenarios.js --> end-to-end scenarios to be run by Protractor
106+
karma.conf.js --> config file for running unit tests with Karma
107+
package.json --> Node.js specific metadata, including development tools dependencies
108+
package-lock.json --> Npm specific metadata, including versions of installed development tools dependencies
109+
```
110+
111+
112+
## Testing
113+
114+
There are two kinds of tests in the `angular-seed` application: Unit tests and end-to-end tests.
115+
116+
### Running Unit Tests
117+
118+
The `angular-seed` app comes preconfigured with unit tests. These are written in [Jasmine][jasmine],
119+
which we run with the [Karma][karma] test runner. We provide a Karma configuration file to run them.
120+
121+
* The configuration is found at `karma.conf.js`.
122+
* The unit tests are found next to the code they are testing and have a `.spec.js` suffix (e.g.
123+
`view1.spec.js`).
124+
125+
The easiest way to run the unit tests is to use the supplied npm script:
126+
127+
```
128+
npm test
129+
```
130+
131+
This script will start the Karma test runner to execute the unit tests. Moreover, Karma will start
132+
watching the source and test files for changes and then re-run the tests whenever any of them
133+
changes.
134+
This is the recommended strategy; if your unit tests are being run every time you save a file then
135+
you receive instant feedback on any changes that break the expected code functionality.
136+
137+
You can also ask Karma to do a single run of the tests and then exit. This is useful if you want to
138+
check that a particular version of the code is operating as expected. The project contains a
139+
predefined script to do this:
140+
141+
```
142+
npm run test-single-run
143+
```
144+
145+
146+
<a name="e2e-testing"></a>
147+
### Running End-to-End Tests
148+
149+
The `angular-seed` app comes with end-to-end tests, again written in [Jasmine][jasmine]. These tests
150+
are run with the [Protractor][protractor] End-to-End test runner. It uses native events and has
151+
special features for AngularJS applications.
152+
153+
* The configuration is found at `e2e-tests/protractor-conf.js`.
154+
* The end-to-end tests are found in `e2e-tests/scenarios.js`.
155+
156+
Protractor simulates interaction with our web app and verifies that the application responds
157+
correctly. Therefore, our web server needs to be serving up the application, so that Protractor can
158+
interact with it.
159+
160+
**Before starting Protractor, open a separate terminal window and run:**
161+
162+
```
163+
npm start
164+
```
165+
166+
In addition, since Protractor is built upon WebDriver, we need to ensure that it is installed and
167+
up-to-date. The `angular-seed` project is configured to do this automatically before running the
168+
end-to-end tests, so you don't need to worry about it. If you want to manually update the WebDriver,
169+
you can run:
170+
171+
```
172+
npm run update-webdriver
173+
```
174+
175+
Once you have ensured that the development web server hosting our application is up and running, you
176+
can run the end-to-end tests using the supplied npm script:
177+
178+
```
179+
npm run protractor
180+
```
181+
182+
This script will execute the end-to-end tests against the application being hosted on the
183+
development server.
184+
185+
**Note:**
186+
Under the hood, Protractor uses the [Selenium Standalone Server][selenium], which in turn requires
187+
the [Java Development Kit (JDK)][jdk] to be installed on your local machine. Check this by running
188+
`java -version` from the command line.
189+
190+
If JDK is not already installed, you can download it [here][jdk-download].
191+
192+
193+
## Updating AngularJS and other dependencies
194+
195+
Since the AngularJS framework library code and tools are acquired through package managers (e.g.
196+
npm) you can use these tools to easily update the dependencies. Simply run the preconfigured script:
197+
198+
```
199+
npm run update-deps
200+
```
201+
202+
This will call `npm update` and `npm run copy-libs`, which in turn will find and install the latest
203+
versions that match the version ranges specified in the `package.json` file.
204+
205+
If you want to update a dependency to a version newer than what the specificed range would permit,
206+
you can change the version range in `package.json` and then run `npm run update-deps` as usual.
207+
208+
209+
## Loading AngularJS Asynchronously
210+
211+
The `angular-seed` project supports loading the framework and application scripts asynchronously.
212+
The special `index-async.html` is designed to support this style of loading. For it to work you must
213+
inject a piece of AngularJS JavaScript into the HTML page. The project has a predefined script to help
214+
do this:
215+
216+
```
217+
npm run update-index-async
218+
```
219+
220+
This will copy the contents of the `angular-loader.js` library file into the `index-async.html`
221+
page. You can run this every time you update the version of AngularJS that you are using.
222+
223+
224+
## Serving the Application Files
225+
226+
While AngularJS is client-side-only technology and it is possible to create AngularJS web apps that
227+
do not require a backend server at all, we recommend serving the project files using a local
228+
web server during development to avoid issues with security restrictions (sandbox) in browsers. The
229+
sandbox implementation varies between browsers, but quite often prevents things like cookies, XHR,
230+
etc to function properly when an HTML page is opened via the `file://` scheme instead of `http://`.
231+
232+
### Running the App during Development
233+
234+
The `angular-seed` project comes preconfigured with a local development web server. It is a Node.js
235+
tool called [http-server][http-server]. You can start this web server with `npm start`, but you may
236+
choose to install the tool globally:
237+
238+
```
239+
sudo npm install -g http-server
240+
```
241+
242+
Then you can start your own development web server to serve static files from any folder by running:
243+
244+
```
245+
http-server -a localhost -p 8000
246+
```
247+
248+
Alternatively, you can choose to configure your own web server, such as Apache or Nginx. Just
249+
configure your server to serve the files under the `app/` directory.
250+
251+
### Running the App in Production
252+
253+
This really depends on how complex your app is and the overall infrastructure of your system, but
254+
the general rule is that all you need in production are the files under the `app/` directory.
255+
Everything else should be omitted.
256+
257+
AngularJS apps are really just a bunch of static HTML, CSS and JavaScript files that need to be
258+
hosted somewhere they can be accessed by browsers.
259+
260+
If your AngularJS app is talking to the backend server via XHR or other means, you need to figure
261+
out what is the best way to host the static files to comply with the same origin policy if
262+
applicable. Usually this is done by hosting the files by the backend server or through
263+
reverse-proxying the backend server(s) and web server(s).
264+
265+
266+
## Continuous Integration
267+
268+
### Travis CI
269+
270+
[Travis CI][travis] is a continuous integration service, which can monitor GitHub for new commits to
271+
your repository and execute scripts such as building the app or running tests. The `angular-seed`
272+
project contains a Travis configuration file, `.travis.yml`, which will cause Travis to run your
273+
tests when you push to GitHub.
274+
275+
You will need to enable the integration between Travis and GitHub. See the
276+
[Travis website][travis-docs] for instructions on how to do this.
277+
278+
279+
## Contact
280+
281+
For more information on AngularJS please check out [angularjs.org][angularjs].
282+
283+
284+
[angularjs]: https://angularjs.org/
285+
[git]: https://git-scm.com/
286+
[http-server]: https://github.com/indexzero/http-server
287+
[jasmine]: https://jasmine.github.io/
288+
[jdk]: https://wikipedia.org/wiki/Java_Development_Kit
289+
[jdk-download]: http://www.oracle.com/technetwork/java/javase/downloads
290+
[karma]: https://karma-runner.github.io/
291+
[local-app-url]: http://localhost:8000/index.html
292+
[node]: https://nodejs.org/
293+
[npm]: https://www.npmjs.org/
294+
[protractor]: http://www.protractortest.org/
295+
[selenium]: http://docs.seleniumhq.org/
296+
[travis]: https://travis-ci.org/
297+
[travis-docs]: https://docs.travis-ci.com/user/getting-started

0 commit comments

Comments
 (0)