var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
// Change this
// TODO make this demo interactive from view
$scope.myScopeVariable = true;
$scope.send_message = function () {
alert ("message.text");
}
});
app.directive('myDirective', ['$compile', '$http', '$templateCache',function($compile, $http, $templateCache) {
var getTemplate = function(contentType) {
var templateLoader,
templateMap = {
ok: 'ok.html',
not_ok: 'not_ok.html',
};
var templateUrl = templateMap[contentType];
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;
}
var linker = function(scope, element, attrs) {
var template = (attrs.isOk)?'ok':'not_ok';
var loader = getTemplate(template);
var promise = loader.success(function(html) {
element.html(html);
}).then(function (response) {
element.replaceWith($compile(element.html())(scope));
});
}
return {
restrict: 'E',
templateUrl: 'not_ok.html',
link: linker
};
}]);
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.js" data-semver="1.2.17"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>myValue <b>{{myValue}}</b></p>
<my-directive is-ok="{{myScopeVariable}}"></my-directive>
<form>
<input type="text" ng-model="bodymessage.text">
<button ng-click="send_message(bodymessage)">Submit</button>
</form>
</body>
</html>
/* Put your css in here */
.true {
color:green;
}
.false {
color:blue;
}
<div class="true">
<div>OK</div>
<form>
<input type="text" ng-model="message.text">
<button ng-click="send_message(message)">Submit</button>
</form>
</div>
<div class="false">NOT OK</div>