码迷,mamicode.com
首页 > 数据库 > 详细

[AngularJS] Accessing The View-Model Inside The link() When Using controllerAs

时间:2015-09-09 06:14:24      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

If u using controller & controllerAs in directive, then the link()‘s 4th param ‘controller‘ will refer to the controller u defined before.

 

 function MessageController(){
  var vm = this;
  
  vm.message = "Hello";
 }

function greeting(){
    function link(scope, element, attrs, ctrl){
       ctrl.message = ctrl.message + ‘ ‘ + scope.name;
    }
  
    return {
      controller: ‘MessageController‘,
      controllerAs: ‘vm‘,
      link: link,
      scope: {
        name: ‘@‘
      },
      template: ‘<h1>{{vm.message}}</h1>‘
    };
}

angular.module(‘app‘, [])
  .directive(‘greeting‘, greeting)
  .controller(‘MessageController‘, MessageController);

 

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="app">
<greeting name="Zhentian"></greeting>
</body>
</html>

 

[AngularJS] Accessing The View-Model Inside The link() When Using controllerAs

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4793467.html

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