码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS中的ng-controller是什么东东

时间:2016-02-05 11:51:23      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 

在AngularJS中,ng-controller是最常用的directive。比如:

 

var app = angular.module("app",[]);
app.controlle("AppCtrl", function(){
    var app = this;
    app.people = [
        {"firstName":"", "lastName":""},
        ...
    ];
})

app.controller("FirstCtrl", function(){
    var first = this;
    first.message = "i am the first one";
})

app.controller("SecondCtrl", function(){
    var second = this;
    second.message = "i am the second one";
})

 

页面部分:

 

<body ng-controller="AppCtrl as app">
    <div ng-controller="FirstCtrl as first">
        {{first.message}}
    </div>
    <div ng-controller="SecondCtrl as second">
        {{second.message}}
    </div>
</body>

 

现在模拟一个ng-controller的directive

 

app.directive("myController", function(){
    return {
        scope: true,
        controller: ‘@‘
    }
})

 

可见,my-controller和ng-controlller工作原理是一样的。

页面部分使用my-controller

 

<body my-controller="AppCtrl as app">
    <div my-controller="FirstCtrl as first">
        {{first.message}}
    </div>
    <div my-controller="SecondCtrl as second">
        {{second.message}}
    </div>
</body>

 

所有的页面呈现都不变。

如果我们想让自定义的my-controller替代AngularJS默认的ng-controller,可以使用priority属性:

 

app.directive("myController", function(){
    return {
        scope: true,
        controller: ‘@‘,
        priority: 500
    }
})

 

默认的priority字段值是0.

 

AngularJS中的ng-controller是什么东东

标签:

原文地址:http://www.cnblogs.com/darrenji/p/5182905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!