Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何将更多值从HTML传递到指令内部?_Angularjs - Fatal编程技术网

Angularjs 如何将更多值从HTML传递到指令内部?

Angularjs 如何将更多值从HTML传递到指令内部?,angularjs,Angularjs,我有一个指示: app.directive('testDir', [function () { return { require: '?ngModel', link: function ($scope, elm, attr, ngModel) { var abc=<some string passed from the html>; } }; }]) app.directive('testDir

我有一个指示:

app.directive('testDir', [function () {
    return {
        require: '?ngModel',
        link: function ($scope, elm, attr, ngModel) {
            var abc=<some string passed from the html>;
        }
    };
}])
app.directive('testDir',[function(){
返回{
要求:“?ngModel”,
链接:功能($scope、elm、attr、ngModel){
var abc=;
}
};
}])
我希望指令是这样的:

<div testDir='abcd'>xx</div>
xx

在指令内时,如何读取值“abcd”?

您可以通过
attr

app.directive('testDir', [function () {
    return {
        require: '?ngModel',
        link: function (scope, elm, attr, ngModel) {
            var abc=attr.testDir;
        }
    };
}]);
或者通过创建隔离作用域(使用'test dir=“'test'))

注: 您的
错误,您需要
testDir
而不是
testDir

<div test-dir='abcd'>xx</div>
xx

通过
attrs
变量(链接过程中的第三个参数)

如果需要传递AngularJS上下文变量,则需要使用$parse

var abc= $parse(attr.testDir)(scope)
谢谢:-)我接受答案。我还有一个关于多个参数的问题。我将为此发布另一个问题。
var abc =  attr.testDir
var abc= $parse(attr.testDir)(scope)