【问题标题】:Why cant we use JS standard methods in AngularJS为什么我们不能在 AngularJS 中使用 JS 标准方法
【发布时间】:2018-01-04 11:25:53
【问题描述】:

这是我的html

<div ng-controller="simpleAppCtrl">
    <p custom-directive ></p>

</div>

这是我的 js

angular.module("simpleApp", [])
        .directive("customDirective", ()=>{
            return (scope, element,attributes)=>{
                element.css("width","100px");
                element.css("border","2px solid red");
                element.css("height","100px");
                element.style.height = "500px";
        }
        })
        .controller("simpleAppCtrl", $scope =>{

        });

所以我使用 jqLit​​e .css 方法向指令中的元素添加了一些样式,并且它起作用了,但是使用 Javascript STYLE 方法添加样式会返回一个错误,指出 STYLE 未定义。同样在指令中我不能在元素上使用 APPENCHILD 方法,但我可以使用 APPEND (这又是 jqLit​​e 方法)。那么为什么我们不能使用大多数 JS 普通函数而需要使用 jquery 呢?

【问题讨论】:

    标签: javascript jquery css angularjs


    【解决方案1】:

    element 是 jqLit​​e 对象,因此 .css() 有效。要使用 DOM 元素,首先使用 [0] 获取其引用,然后您可以操作其样式属性。

    element[0].style.height = "500px";
    

    【讨论】:

    • 所以每次我想做一些操作时,我都需要参考 [0] ,或者使用 jqLit​​e 方法。谢谢你给的信息。虽然我仍然很奇怪他们为什么这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2012-07-24
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多