1

以下是我的控制器代码:

app.controller('HomeController', ['$scope', '$location', '$sce', '$routeParams', '$timeout', function($scope, $location, $sce, $routeParams, $timeout ) {
    // Configuration Variables:
    $scope.title = "Site Title";
    $scope.logosrc = "images/site_logo.gif";
    $scope.tagline = "This is the new tagline!!!";

    // This changes the shadowed aspect of the top nav and footer based on a checkbox
    $scope.shadowChange = function(cb){
        alert ("hi");
        return true;
    };

    // This converts any HTML code in the text to actual code so that it renders properly
    $scope.renderHtml = function(html_code)
    {
        return $sce.trustAsHtml(html_code);
    };

}]);

我在我的代码中使用 onClick 复选框调用 shadowChange 函数,如下所示:

<input type="checkbox" name="shadow" id="shadow" checked onClick="shadowChange(this);" />

我不仅将 HomeController 绑定到 body 标签,我还(以防万一)使用 HomeController 将相关代码包装在另一个 div 中,Chrome Inspector 甚至告诉我 HomeController 加载正常。

此外,控制器顶部的那些配置变量正在正确设置。

然而,当我单击复选框时,我收到一个错误,即 shadowChange 未定义。如果我尝试(仅用于测试)那里的另一个函数,renderHtml,我会得到同样的错误。然而,在我的另一个应用程序中,我以相同的方式使用它并且效果很好。

我看不到是什么阻止了代码看到这些功能。

想法?

4

1 回答 1

2

您想使用该ng-click指令来访问控制器的范围。(而不是onClick

<input type="checkbox" name="shadow" id="shadow" checked ng-click="shadowChange(this);" />
于 2015-12-08T18:11:54.033 回答