├── LICENSE ├── README.md └── angularjs.go /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Richard Musiol. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-angularjs 2 | ------------ 3 | A wrapper for [AngularJS](http://angularjs.org) to be used with [GopherJS](https://github.com/gopherjs/gopherjs). 4 | 5 | *This project is not maintained any more. Suggested forks:* 6 | 7 | - https://github.com/wvell/go-angularjs/ 8 | -------------------------------------------------------------------------------- /angularjs.go: -------------------------------------------------------------------------------- 1 | package angularjs 2 | 3 | import "github.com/gopherjs/gopherjs/js" 4 | 5 | type Module struct{ *js.Object } 6 | 7 | func (m *Module) NewController(name string, constructor func(scope *Scope)) { 8 | m.Call("controller", name, js.S{"$scope", func(scope *js.Object) { 9 | constructor(&Scope{scope}) 10 | }}) 11 | } 12 | 13 | type Scope struct{ *js.Object } 14 | 15 | func (s *Scope) Apply(f func()) { 16 | s.Call("$apply", f) 17 | } 18 | 19 | func (s *Scope) EvalAsync(f func()) { 20 | s.Call("$evalAsync", f) 21 | } 22 | 23 | type JQueryElement struct{ *js.Object } 24 | 25 | func (e *JQueryElement) Prop(name string) *js.Object { 26 | return e.Call("prop", name) 27 | } 28 | 29 | func (e *JQueryElement) SetProp(name, value interface{}) { 30 | e.Call("prop", name, value) 31 | } 32 | 33 | func (e *JQueryElement) On(events string, handler func(*Event)) { 34 | e.Call("on", events, func(e *js.Object) { 35 | handler(&Event{Object: e}) 36 | }) 37 | } 38 | 39 | func (e *JQueryElement) Val() *js.Object { 40 | return e.Call("val") 41 | } 42 | 43 | func (e *JQueryElement) SetVal(value interface{}) { 44 | e.Call("val", value) 45 | } 46 | 47 | type Event struct { 48 | *js.Object 49 | KeyCode int `js:"keyCode"` 50 | } 51 | 52 | func (e *Event) PreventDefault() { 53 | e.Call("preventDefault") 54 | } 55 | 56 | func NewModule(name string, requires []string, configFn func()) *Module { 57 | return &Module{js.Global.Get("angular").Call("module", name, requires, configFn)} 58 | } 59 | 60 | func ElementById(id string) *JQueryElement { 61 | return &JQueryElement{js.Global.Get("angular").Call("element", js.Global.Get("document").Call("getElementById", id))} 62 | } 63 | 64 | func Service(name string) *js.Object { 65 | return js.Global.Get("angular").Call("element", js.Global.Get("document")).Call("injector").Call("get", name) 66 | } 67 | 68 | type HttpService struct{} 69 | 70 | var HTTP = new(HttpService) 71 | 72 | func (s *HttpService) Get(url string, callback func(data string, status int)) { 73 | future := Service("$http").Call("get", url) 74 | future.Call("success", func(data string, status int, headers *js.Object, config *js.Object) { 75 | callback(data, status) 76 | }) 77 | future.Call("error", func(data string, status int, headers *js.Object, config *js.Object) { 78 | callback(data, status) 79 | }) 80 | } 81 | --------------------------------------------------------------------------------