L
L
LeshaPistolet2014-09-11 13:58:31
JavaScript
LeshaPistolet, 2014-09-11 13:58:31

Why doesn't ng-click work when generating html when from a jQuery function?

for (var i = 0; i < categories.length; i++) {
                if (categories[i]=="Home") {
                    $("#categoryNav").append("<a class=\"btn btn-primary\"  ng-click=\"app.changeList('')\">" + categories[i] + "</a>");
                }
                else {
                    $("#categoryNav").append("<a class=\"btn btn-primary\"  ng-click=\"app.changeList('" + categories[i] + "')\">" + categories[i] + "</a>");
                }      
            }

Generates buttons, each of which calls a method in the angularjs controller:
app.controller("ProductsController", function ($http) {
        var goga = this;
   goga.changeList = function (category) {
            $http.get('/api/DataService/' + category)
                 .success(function (results) {
                     goga.products = results;
                 });
        };
    });

But when you click on the button, nothing happens, i.e. the method call is not made. However, if you create a button manually and set a parameter when calling the goga.changeList('Soccer') method, then the call occurs. The code of a button written by hand and the code generated by a function do not differ. What is the problem?

An example of a button created manually:
<a class="btn btn-primary" ng-click="app.changeList('Soccer')">TEST</a>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
TekVanDo, 2014-09-11
@LeshaPistolet

Most likely the problem is not in the correct sequence of script execution, i.e. angular is initialized faster than jquery generates buttons.
You are clearly using angular incorrectly and the buttons need to be built using angular.

A
Alexander Tartmin, 2014-09-11
@baskerville42

Are you using noConflict()? And then you can’t say so in your code. jQuery and angular don't get along

F
FuzzyBases, 2014-10-19
@FuzzyBases

Angular has a provider called $compile. https://docs.angularjs.org/api/ng/service/$compile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question