Skip to content

Instantly share code, notes, and snippets.

@lagden
Created June 28, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lagden/45d14cadbf8045d24c3a to your computer and use it in GitHub Desktop.
Save lagden/45d14cadbf8045d24c3a to your computer and use it in GitHub Desktop.
A Pen by Thiago Lagden.
h1 RequireJS + Angular
p Using a service to make the communication between controllers.
//- Trigger Controller
div(ng-controller="triggerController")
input(ng-model="message", placeholder="valor", ng-change="update(message)")
hr
//- Foo Controller
div(ng-controller="fooController")
p valor * 5 = {{message}}
//- Bar Controller
div(ng-controller="barController")
p valor + 5 = {{message}}

Requirejs + Angular

An example of how you can use Angular with Requirejs. Plus: Using a service to make the communication between controllers

A Pen by Thiago Lagden on CodePen.

License.

# Config
requirejs.config
paths:
angular: "//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.16/angular.min"
shim:
angular:
exports: "angular"
# Require
require [
"angular"
"msg"
"trigger"
"foo"
"bar"
], (angular, Msg, trigger, foo, bar) ->
exemplo = angular.module("exemplo", [])
exemplo
.service("Msg", Msg)
.controller("triggerController", trigger)
.controller("fooController", foo)
.controller "barController", bar
angular
.bootstrap document, ["exemplo"]
return
# Modulo msg -> Service
define "msg", ->
"use strict"
class Msg
constructor: ->
@handlers = []
Msg::on = (eventName, handler) ->
@handlers.push
eventName: eventName
handler: handler
return
Msg::emit = (eventName, args) ->
h.handler args for h in @handlers when h.eventName is eventName
return
return [Msg]
# Modulo Trigger -> Controller
define "trigger", ->
"use strict"
class trigger
constructor: ($scope, Msg) ->
$scope.update = (n) ->
Msg.emit "updateMsg", n
return [
"$scope"
"Msg"
trigger
]
# Modulo Foo -> Controller
define "foo", ->
"use strict"
class foo
constructor: ($scope, Msg) ->
$scope.message = ""
Msg.on "updateMsg", (n) ->
n = parseInt(n, 10) or 0
$scope.message = n * 5
return [
"$scope"
"Msg"
foo
]
# Modulo Bar -> Controller
define "bar", ->
"use strict"
class bar
constructor: ($scope, Msg) ->
$scope.message = ""
Msg.on "updateMsg", (n) ->
n = parseInt(n, 10) or 0
$scope.message = n + 5
return [
"$scope"
"Msg"
bar
]
body
padding: 40px
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment