&操作员未在指令的隔离范围内工作

& operator not working in the isolated scope of directive

本文关键字:隔离 范围内 工作 指令 amp 操作员      更新时间:2023-09-26

我正在使用&(表达式绑定)运算符,但我无法在父控制器上触发函数。控制台上应该有输出,但我没有收到任何输出。

在HTML部分:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Directive &</title>
  <script type="text/javascript" src="angular.min.js"></script>
  <script src="isolate_scope_&.js"></script>  
    </head>
    <body ng-app="isolate_scope">
        <div ng-controller="isolateScopeController">
          <b>Ctrl Data</b>
          <div ng-repeat="person in persons">
              <p>{{person.name}}</p>
          </div>
          <b>Directive Data</b>
          <div ng-repeat="person in persons">
              <friends frnd="person.name"></friends>
          </div>
          <my-button isolatedFunction="printScopeToFile()"></my-button>
        </div>
    </body>
    </html>

JS部分如下:

  angular.module('isolate_scope', [])
    .controller('isolateScopeController', function($scope){
      $scope.persons = [
                         {
                           name:"tanmay",
                           age:"28"
                         },
                         {
                           name: "James",
                           age:"28"
                         },
                         {
                           name:"Rylan",
                           age:"26"
                         },
                         {
                            name:"Aditya",
                            age:"23"
                         }
                       ];
      $scope.printScopeToFile = function(){
          console.log("printng to file.....");
          for(var i in $scope.persons){
            console.log("Name = " + $scope.persons[i].name + " Age = " + $scope.persons[i].age);
          }
      };
    })
    .directive('friends',function(){
      return {
          restrict :'E',
          template: '<input type="text" ng-model="name">',
          scope :{
            name:'=frnd'
          }
      };
    })
    .directive('myButton',function(){
      return {
          restrict: 'E',
          template: '<button ng-click="isolatedFunction()">callParentfunction</button>',
          scope : {
            isolatedFunction:"&"
          }
      };
    });

同样的:http://jsfiddle.net/v51kob1q/

<my-button isolatedFunction...>指令中的属性isolatedFunction应该是isolated-functiondash-delimited属性