2017-03-08 61 views
-1

我需要綁定在angularjs自定義事件(1.x中)和我試着用下面的代碼,如何在angularjs中綁定多個自定義事件?

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
<link href="https://www.polymer-project.org/components/polymer/polymer.html" rel="import"> 
<link href="https://www.polymer-project.org/components/paper-button/paper-button.html" rel="import"> 
<div ng-app="demo-app"> 
    <div ng-controller="DemoController"> 
     <template bind-angular-scope is="auto-binding"> 
      <paper-button raised on-tap="{{clickMe}}" on-mouseover="{{mouseOver}}">click me</paper-button> 
     </template> 
     <pre><code>{[{text}]}</code></pre> 
    </div> 
</div> 

腳本

<script> 
angular.module('demo-app', []) 
    .config(function ($interpolateProvider) { 
     $interpolateProvider.startSymbol('{[{').endSymbol('}]}'); 
    }) 
    .directive('bindAngularScope', function() { 
     return { 
      restrict: 'A', 
      link: function (scope, element, attrs) { 
       for (k in scope) { 
        if (!element[0][k]) { 
         element[0][k] = scope[k]; 
        } 
       } 
      } 
     } 
    }) 
    .controller('DemoController', function ($scope) { 
     $scope.text = ''; 
     $scope.clickMe = function() { 
      $scope.text += '\nyou clicked me!!'; 
      $scope.$apply(); 
     }; 
     $scope.mouseOver = function() { 
      $scope.text += '\nyou hovered me!!'; 
      $scope.$apply(); 
     } 
    }); 
</script> 

這不是working.Could你指出我的問題或是否有任何解決方案綁定自定義事件(多)?我們是否需要爲每個人創建自定義指令?

注:

上面的代碼是從以下網址提到,提前

How to bind custom events in AngularJS?

謝謝!

+1

什麼是錯誤?爲什麼它不起作用?分享一些代碼 –

+0

運行代碼片段本身不起作用。請看「穆罕默德瓦利德」提供的答案,這是更好的解決方案嗎? –

回答

0
angular.module('demo-app', []) 
    .config(function ($interpolateProvider) { 
     $interpolateProvider.startSymbol('{[{').endSymbol('}]}'); 
    }) 
    .directive('bindAngularScope', function() { 
     return { 
      restrict: 'A', 
      link: function (scope, element, attrs) { 
       for (k in scope) { 
        if (!element[0][k]) { 
         element[0][k] = scope[k]; 
        } 
       } 

       elem.bind('click', function() { 
       /* Place your click logic here */
       }); 
      } 
     } 
    }) 
相關問題