本文介绍了生成(编译)NG-点击按钮扔"范围适用于$不是一个函数&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被玩弄这一点,得到一个非常奇怪的错误。
我不会把整个code在这里,因为有办法不多spagetti所有的地方,但我想我可以隔离code。

Been playing around with this a bit, and get a very strange error. I will not put the whole code here, because there are way to much spagetti all over the place, but I think I can isolate the code.

我有一些code,其基于从数据库中某些变量在屏幕上一堆按钮。天色链接,但我想保存数据库中的什么就用它做什么的信息。所以我想我的功能:
从数据库中获取数据(完成)
寻找一些额外的条目,二进制声音和图像(完成)
绘制pretty按钮(它不是pretty,但它是一个按钮)
创建当用户presses的按钮NG-click事件(第1步完成)
编译HTML理清了NG-点击DOM(运行...)

I have some code that generates a bunch of buttons on the screen based on some variables from the database. Mostly links, but I want the database to hold information on what to do with it. So I want my function to:Get data from the database (done)Find some additional entries, binary sound and image (done)Draw a pretty button (it ain't pretty, but it is a button)Create a ng-click event for when the user presses the button (1st step done)compile the html to sort out the dom for ng-click (it runs...)

这一切似乎工作,我得到的屏幕上的按钮,但是当我preSS它,我得到以下错误信息:

All this seems to work, and I get a button on the screen, but when I press it I get the below error message:

Uncaught TypeError: scope.$apply is not a function

在code,做它(简体):

The code that does it (simplified):

            var newPad = '<button class="padbtn" ';
            newPad = newPad + 'data-ng-click="'+dataPad.comSound+'('+dataPad.typeSound+','+padPos+')">';
            newPad = newPad + '<img class="padImg" src="/api/getImg/'+dataPad.srcImage+'" style="width:175px; height 175px;"/>'
            newPad = newPad + '<br/>'+dataPad.name+'</button>';
            console.log(newPad);
            var padElement = $compile(newPad)(true);
            console.log(padElement);
            $('#cell'+padPos).append(padElement);

要更简单重现(和自包含)

To make it simpler to reproduce (and self contained)

var newPad = '<button class="padbtn" data-ng-click="playBeat(smpBeat,C1)"><img class="padImg" src="/api/getImg/554240a17b221e1a2ffda099" style="width:175px; height 175px;"/><br/>Clap</button>'
                var padElement = $compile(newPad)(true);
            $('#targetID').append(padElement);

照片和一切加载,但一旦我preSS的按钮,我得到这个错误消息。

Picture and everything loads, but once I press the button I get that error message.

推荐答案

在这里,你必须编译 $范围

    var newPad = '<button class="padbtn" data-ng-click="playBeat(smpBeat,C1)"><img class="padImg" src="/api/getImg/554240a17b221e1a2ffda099" style="width:175px; height 175px;"/><br/>Clap</button>'
    var padElement = $compile(newPad)($scope);
    $('#targetID').append(padElement);

查看工作示例

这篇关于生成(编译)NG-点击按钮扔&QUOT;范围适用于$不是一个函数&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:00