无法在 angularjs 控制器中点击 ng-click?

Unable to hit ng-click in angularjs controller?

我在app.js中定义了不同的控制器和相应的模板url :

var App = angular.module('FormApp', [ 'ngRoute','ui.bootstrap', 'dialogs', 'oc.modal' ]);

App.config([ '$routeProvider', function($routeProvider) {
    $routeProvider.when('/addClient', {
        templateUrl : '../../resources/partialHtml/addClientLayout.html',
        controller : FormController
    }).when('/conflist/:commandName/:client/:env', {
        templateUrl : '../../resources/partialHtml/testPrase.html',
        controller : TestParseController
    }).when('/addNewCommand', {
        templateUrl : '../../resources/partialHtml/addNewCommand.html',
        controller : AddNewCommandController
    })
} ]);

我的 TestParseController 定义如下:

var TestParseController = function($scope, $window, $http, $routeParams, $sce,
        $compile) {

    $scope.hide = function(obj) {
        alert($routeParams.commandName);
    };

    $scope.to_trusted1 = function(html_code) {
        html_code = $sce.trustAsHtml(html_code);
        $scope.content = html_code;
        alert(html_code);
        $compile( document.getElementById('innerh'))($scope);
    };

    $http.get('client/getConfList/' + $routeParams.commandName)
    .success(
            function(data) {
            $scope.html_content = "<button data-ng-click='hide($event)'>Click me!</button>";
            $scope.to_trusted1($scope.html_content);
                });
}

Html : testParse.html :

<h3 data-ng-click="hide($event)" class="plus">Add</h3>

<div ng-bind-html="content" id="innerh">    </div>

div 已正确填充,但填充按钮的 ng-click 不起作用,但它适用于页面本身可用的 h3 标签。

有人请帮忙..

更改填充 innerh div 的方式,使用以下内容代替 $scope.content(并从 innerh [=45] 中删除 ng-bind-html =]):

document.getElementById('innerh').innerHTML = html_code;

进入to_trusted1函数:

 $scope.to_trusted1 = function(html_code) {
        html_code = $sce.trustAsHtml(html_code);
        $scope.content = html_code;
        //alert(html_code);
        document.getElementById('innerh').innerHTML = html_code;

  };

jsfiddle: https://jsfiddle.net/m37xksxk/

解决方案 2:

更 AngularJS 的方法可能是使用 $timeout.

您可以使用它来确保内容包含在 div 中。您还必须获取 div 中的内容,因为这是将要编译的内容,其中:angular.element(document.getElementById('innerh')).contents():

所以会变成:

    $timeout( function(){ 
        $compile( angular.element(document.getElementById('innerh')).contents())
           ($scope);
        }, 0);

jsfiddle 2: https://jsfiddle.net/m37xksxk/1/

您有没有创建指令的原因?您的实施非常混乱。这是我在 jsfiddle 中创建的内容:

http://jsfiddle.net/5qa6uy2y/

app.directive('testContent', function () {
    return {
        template: "<button data-ng-click='hide($event)'>Click me!</button>"
    };
});