微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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指令来实现这一点.

原文地址:https://www.jb51.cc/angularjs/142339.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐