2016-06-30 9 views
0

Angularjsで CodeigniterコントローラからAjaxレスポンスを取得しました。そのレスポンスをHTMLページに割り当てるにはどうすればいいのですか?anglejsでngclickがajaxレスポンスで動作していない

アドバイスします。

+0

$スコープにHTMLをコンパイルするために、$ compileを使用してください。その後に動作する必要があります –

答えて

1

うわー、私は答えを得ました。

角度スクリプト:

var myApp = angular.module('myApp', []); 
    myApp.controller('MyCtrl', function($scope) { 
      $scope.buttonHtml = "<a ng-click='samlpleMessage("sample123")'>click me</a>"; 
      $scope.samlpleMessage = function(message) { 
       alert(message); 
      } 

     }); 
    myApp.directive('compile', ['$compile', function ($compile) { 
     return function(scope, element, attrs) { 
      scope.$watch(
      function(scope) { 
       // watch the 'compile' expression for changes 
       return scope.$eval(attrs.compile); 
      }, 
      function(value) { 
       // when the 'compile' expression changes 
       // assign it into the current DOM 
       element.html(value); 

       // compile the new DOM and link it to the current 
       // scope. 
       // NOTE: we only compile .childNodes so that 
       // we don't get into infinite loop compiling ourselves 
       $compile(element.contents())(scope); 
      } 
     ); 
     }; 
    }]); 

私は、角スクリプトに関数をコンパイル追加しました。私はこのコードを追加しました。

<div ng-controller="MyCtrl"> 
    <div compile="buttonHtml"></div> 
</div> 

この問題を解決しました。 ありがとう:)

関連する問題