angularjs – Angular TypeError:name.replace不是ng-style的函数

我是角色的新手并且在控制台TypeError中不断收到以下错误:name.replace不是函数.我不确定究竟是什么导致它,但它似乎是由ng风格的声明引起的,也许与camelCase有关?

我不明白的部分是为什么ng-style =“isFrontView()||!匹配&& {‘display’:’none’}”抛出错误,但ng-style =“!isFrontView()| |!matches&& {‘display’:’none’}“不会抛出错误.

为了解决这种情况,我尝试从函数名中删除camelCase并全部小写.我也尝试使用!! isFrontView(),但似乎都没有删除错误消息.

有谁知道这个错误消息的原因和潜在的修复?

HTML模板:

<div class="system-view">
    <div class="controller-container fill" id="systemView1" ng-style="isFrontView() || !matches && {'display': 'none'}">
        <canvas id="canvasLayer-shell" data-layername="front" width="617" height="427"></canvas>
        <i ng-if="!matches" class="fa fa-repeat toggle-view" ng-click="changeView()" ng-touch="changeView()"></i>
    </div>
    <div class="controller-container fill" id="systemView2" ng-style="!isFrontView() || !matches && {'display': 'none'}">
        <canvas id="canvasLayer-shell" data-layername="back" width="617" height="427"></canvas>
        <i ng-if="!matches" class="fa fa-undo toggle-view" ng-click="changeView()" ng-touch="changeView()"></i>
    </div>
</div>

后端代码

$scope.frontView = true;
$scope.matches = true;

$scope.isFrontView = function() {
   return $scope.frontView;
};

$scope.changeView = function() {
    $scope.frontView = !$scope.frontView;
};

附:即使控制台出错,一切仍然正常.

您的潜在问题是由于ng-style的使用不正确. ng-style sets a watcher on the expression并设置 element’s style with the help of jquery/jqlite element.css.并且inside element.css css属性(name)被转换为标准的驼峰套管( which uses regex string replace).在您的特定情况下,表达式求值为boolean(true)而不是对象( ng-style does this for each property),boolean没有replace属性(在字符串对象上可用),因此失败.您可以通过使用字符串连接将表达式转换为求值来测试此值.

即ng-style =“”(isFrontView()||!匹配&& {‘display’:’none’})”

查看表达式所有你需要它来隐藏和显示元素,你可以使用ng-show/ng-hide指令来实现这一点.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...