【发布时间】:2015-01-10 01:22:05
【问题描述】:
我有一个带有 angularjs 和 jquery 的应用程序。我在以下 angularjs 脚本中有一些 jquery:
//allow co-existence of jquery and angular
var $jq = jQuery.noConflict();
//initialize an angular scoped variable taht will be manipulated and used by jquery object
$scope.myOption1 = true;
//jquery ready function
$jq(function() {
var myGadget = $jq('.gadget-container').gadget({
option1: $scope.myOption1
});
});
//angular function to manipulate the jquery object (which represents an image slider)
$scope.myFunction = function() {
//change myOption1 to false
$scope.myOption1 = false;
//re-initialize myGadget with new myOption1 value
myGadget.reInit(); //reInit is a built-in function for myGadget to reinitialize the gadget when options change
}
Jquery 可以访问 $scope.myOpyion1 就好了。但是当角度函数 $scope.myFunction 运行时,角度会抛出错误“myGadget is not defined”。我明白为什么 - 它正在寻找角度变量,而不是 jquery 变量。那么如何在角度函数中访问 jquery 变量 myGadget 呢?
【问题讨论】: