/** * Created by Answer1215 on 12/21/2014.*/Angular.module (' App ', []). Controller (' Firstctrl ',function(){ varVM = This; Vm.message= "I am the first controller"; }). controller (' Secondctrl ',function() { varVM = This; Vm.message= "I AM the second controller"; }). directive (' Customercontroller ',function() { return{scope:true,//Create a new scopeController: ' @ ',//assing the directive name to this controllerpriority:500, restrict:A } })
<!DOCTYPE HTML><HTMLNg-app= "App"><HeadLang= "en"> <MetaCharSet= "UTF-8"> <title></title> <Linkrel= "stylesheet"href= "Bower_components/bootstrap/dist/css/bootstrap.min.css"/></Head><Body> <DivCustomer-controller= "Firstctrl as first"class= "Well">{{First.message}}</Div> <DivCustomer-controller= "Secondctrl as second"class= "Well">{{Second.message}}</Div><Scriptsrc= "Bower_components/angular/angular.min.js"></Script><Scriptsrc= "App.js"></Script></Body></HTML>
In angular.js src:
var ngcontrollerdirective = [function() { return { ' A ', true , ' @ ', $ ' ;}];
This. directive =functionregisterdirective (name, directivefactory) {assertnothasownproperty (name,' directive '); if(isstring (name)) {Assertarg (directivefactory,' Directivefactory '); if(!Hasdirectives.hasownproperty (name)) {Hasdirectives[name]= []; $provide. Factory (name+ Suffix, [' $injector ', ' $exceptionHandler ', function($injector, $exceptionHandler) {vardirectives = []; ForEach (Hasdirectives[name],function(directivefactory, index) {Try { vardirective =$injector. Invoke (Directivefactory); if(isfunction (Directive)) {Directive={compile:valuefn (Directive)}; } Else if(!directive.compile &&directive.link) {directive.compile=Valuefn (Directive.link); } directive.priority= Directive.priority | | 0; Directive.index=index; Directive.name= Directive.name | |name; Directive.require= Directive.require | | (Directive.controller &&directive.name); Directive.restrict= Directive.restrict | | ' EA '; if(IsObject (Directive.scope)) {directive.$ $isolateBindings=parseisolatebindings (Directive.scope, directive.name); } directives.push (Directive); } Catch(e) {$exceptionHandler (e); } }); returndirectives; }]); } hasdirectives[name].push (Directivefactory); } Else{ForEach (name, Reverseparams (registerdirective)); } return This; };
[AngularJS] Build Your Own Ng-controller Directive