首页 > 如何实现angular中link中的数据和controller中的数据相互绑定

如何实现angular中link中的数据和controller中的数据相互绑定

1,link中的数据没办法和controller中的数据完成相互绑定

<html >
  <head>
    <meta charset="UTF-8">
    <title>Bootstrap Modal and popover with Velocity.js animation</title>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
    <!-- <link rel="stylesheet" type="text/css" href="elfinderCtroller.js"> -->
  </head>
  <body>
      <div ng-app = "app"  ng-controller="elfinder" name = "mingzi">
        Name: <input  ng-model="mingzi">
        <p>{{mingzi}}</p>
        <texts names></texts>
    </div>
    <script> 
        var app = angular.module('app', []);

        app.controller('elfinder',function($scope){
            $scope.mingzi = "caa";
        })
        app.directive('texts', function(){
            return{
                restrict:'EC',
                scope: {
                    name : "=",
                },
                // controller : 'elfinder',
                link: function(scope) {
                      scope.name = 'fwt';
                }
            };
        });
    </script>

  </body>
</html>

2


写法没对,改一下就好了:

<html >
  <head>
    <meta charset="UTF-8">
    <title>Bootstrap Modal and popover with Velocity.js animation</title>
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
    <!-- <link rel="stylesheet" type="text/css" href="elfinderCtroller.js"> -->
  </head>
  <body>
      <div ng-app = "app"  ng-controller="elfinder">
        Name: <input  ng-model="mingzi">
        <p>{{mingzi}}</p>
        <texts name="mingzi"></texts>
    </div>
    <script> 
        var app = angular.module('app', []);

        app.controller('elfinder',function($scope){
            $scope.mingzi = "caa";
        })
        app.directive('texts', function(){
            return{
                restrict:'E',
                scope: {
                    name : "=",
                },
                // controller : 'elfinder',
                link: function(scope) {
                      console.log(scope.name);
                }
            };
        });
    </script>

  </body>
</html>

参考文章:AngularJS内幕详解之 Directive

【热门文章】
【热门文章】