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.
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}} |
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.
# 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 |