单击按钮上的隔离范围不起作用

时间:2015-07-14 09:43:47

标签: javascript angularjs angularjs-directive

我是angularjs的新手。在angularjs中处理指令我遵循了一本书AngularJS指令的例子。从那我尝试了一些样本,但我坚持与我需要在我的应用程序中实现类似的一个示例。

代码是:

<html>
  <head>
    <link rel="stylesheet" href="style.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>    
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-app="directiveapp">

    <!--  Two way binding -->
    <div ng-init="title = 'Hello World'; subtitle = 'I am an app'">
       <h2 id="appTitle">{{title}}</h2>
       <h3 id="appSub">{{subtitle}}</h3>
       <button id="newAppTitle" ng-click="setAppTitle('App 2.0')">Upgrade me!</button>
        <div my-scoped-directive msd-title="I'm a directive, within the app{{title}}" msd-subtitle="subtitle">
          <h4 id="directiveTitle">{{title}}</h4>
          <button id="newDirTitle" ng-click="setDirectiveTitle('bob')">Bobit!</button>
          <button id="newDirSub" ng-click="setDirectiveSubtitle('Time to submerge')">Empty the ballasts!</button>
        </div>
    </div>
</div> <!-- End of directiveApp -->
  </body>
</html>

的script.js

var app = angular.module('directiveapp', []);

app.directive('myScopedDirective', function() {
  return {
      restrict: 'EA',
      scope : {
        'title' : '@msdTitle',
        'subtitle' : '=msdSubtitle'
      },
    link : function ($scope, $element, $attrs) {
    console.log($scope)
    $scope.setDirectiveTitle = function (title) {
       $scope.title = title;
    };

    $scope.setDirectiveSubtitle = function (subtitle) {
        $scope.subtitle = subtitle;
    };
  }
 };
});

问题是当我点击按钮时没有点击事件被触发。文本没有改变。 单击Bobit按钮时,文本应该从Hello world更改为I 指令,在应用程序Hello World

这里我附上了plunker链接:http://plnkr.co/edit/NdnWDqr9XCph6uNvJwlc?p=preview

2 个答案:

答案 0 :(得分:1)

这不是正确的方法,因为应用于元素的指令编译为指令但在该元素内部没有指令的范围,它包含控制器的范围。 的 app.js

  var app = angular.module('directiveapp', []);

    app.directive('myScopedDirective', function() {
      return {
          restrict: 'EA',
          scope : {
            'title' : '@msdTitle',
            'subtitle' : '=msdSubtitle'
          },
          templateUrl:'main.html',
        link : function ($scope, $element, $attrs) {
        console.log($scope)
        $scope.setDirectiveTitle = function (title) {
           $scope.title = title;
        };

        $scope.setDirectiveSubtitle = function (subtitle) {
            $scope.subtitle = subtitle;
        };
      }
     };
    });

查看更新的plunkr link

答案 1 :(得分:1)

使用ng-click可以调用控制器中的某个功能。

把这个:

$scope.setDirectiveTitle = function (title) {
   $scope.title = title;
};

$scope.setDirectiveSubtitle = function (subtitle) {
    $scope.subtitle = subtitle;
};

在父控制器内部或修改指令以拥有自己的控制器

app.directive('myScopedDirective', function() {
  return {
    restrict: 'EA',
    scope : {
      'title' : '@msdTitle',
      'subtitle' : '=msdSubtitle'
    },
    template: '<h4 id="directiveTitle">{{title}}</h4>' +
      '<button id="newDirTitle" ng-click="setDirectiveTitle(\'bob\')">Bobit!</button>'+
      '<button id="newDirSub" ng-click="setDirectiveSubtitle(\'Time to submerge\')">Empty the ballasts!</button>',

    controller: function($scope){
      $scope.setDirectiveTitle = function (title) {
        $scope.title = title;
      };

      $scope.setDirectiveSubtitle = function (subtitle) {
        $scope.subtitle = subtitle;
      };
    }
  };
});