zoukankan      html  css  js  c++  java
  • [AngularJS] Accessing The View-Model Inside The link() When Using controllerAs

    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>
  • 相关阅读:
    周总结
    周总结
    周总结
    读后感
    周总结
    周总结
    周总结
    第一周总结
    大学生失物招领平台使用体验
    快速乘法+快速幂
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4793467.html
Copyright © 2011-2022 走看看