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

查看:63
本文介绍了javascript - 如何实现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

这篇关于javascript - 如何实现angular中link中的数据和controller中的数据相互绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆