点击DOM

时间:2016-11-12 18:49:43

标签: javascript html angularjs

嗨我想在点击加载角度后添加到dom的按钮后调用一个函数。我不知道这是否可能以我尝试的方式,但我知道我当前的代码不会调用警报功能。

向我的后端发送搜索请求后,我得到一个列表数据,我将其显示为hrefs。

        my_form.setAttribute("href", result[i].url)
        my_form.setAttribute("ng-href",'#here')
        my_form.setAttribute("ng-click", "alert(1)")
        my_form.setAttribute("style", "display:block;")

结果

<a href="/#/person/367" ng-href="#here" ng-click="alert(1)" style="display:block;">John Doe</a>

完整代码:

var appModule = angular.module('graph', [])
appModule.controller('graphCtrl', ['$scope', '$http', '$compile', function ($scope, $http, $compile) {
$scope.search = function () {
    var data = {

    }

    $http.get('/api/search/?q=' + $scope.searchfield, data).success(function (data, status, headers, config) {
        var result = data;

        var searchResultsContainer = document.getElementById("dropdownId");
        while (searchResultsContainer.firstChild) {
            searchResultsContainer.removeChild(searchResultsContainer.firstChild);
        }
        for (i in result) {


            var my_form = document.createElement("a"); 
            my_form.setAttribute("href", result[i].url)
            my_form.setAttribute("ng-href",'#here')
            my_form.setAttribute("ng-click", "alert(1)")
            my_form.setAttribute("style", "display:block;")
            my_text = document.createTextNode(result[i].caption)

            my_form.appendChild(my_text)

            searchResultsContainer.appendChild(my_form)

        }
    })
}

  $scope.alert = function(){
        alert("Hello! I am an alert box!!");
    }
}

0 个答案:

没有答案