[AngularJS] Build Your Own Ng-controller Directive

Source: Internet
Author: User

/** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.