指令中的作用域变量未定义
scope variable undefined in directives
我正在使用指令,我想在指令内使用范围变量,但它正在抛出未定义的错误。
我想循环与固定的数字?所以我试着这里的项目是未定义的错误,我得到?
是否有其他方法来迭代循环?多少次??
HTML:<my-dir
bgcolor="#EFF"
my-title="abdullah"
my-height="190"
my-width="160"
my-color="red"
my-bgcolor="blue"
my-collapseoption=true
my-widgetno="1"
save="saveChanges('custom directive')">template
</my-dir>
脚本:
var app = angular.module('myApp', ['ngAnimate']);
app.controller('MyCtrl', function($scope, $http) {
$scope.items = ['a','b','c'];
});
// add a directive
app.directive("myDir", function() {
return {
restrict: "E",
scope: {
items:"=",
myTitle: "@", // by value
myHeight: "@", // by value
myWidth: "@", // by value
myColor: "@", // by value
myBgcolor: "@", // by value
myWidgetno: "@", // by reference
myCollapseoption: "@", // by reference
save: "&" // event
},
templateUrl:"widget.html",
replace: true,
transclude: false,
link: function (scope, element, attrs) {
// show initial values: by-val members will be undefined
console.log("items is " +scope.items);
// change element just to show we can
element.css("background", attrs.myBgcolor);
element.css("color", attrs.myColor);
element.css("width", attrs.myWidth+'px');
// element.css("height", attrs.myHeight+'px');
}
}
});
你从未将你的items属性传递给指令,所以当指令试图访问它时,它肯定是未定义的。my-dir标记需要提供该属性。试一试:
<my-dir
items={{items}}
.... />
相关文章:
- Javascript变量未定义,onchange不起作用
- 定义的变量未定义
- 无法弄清楚为什么本机 Promise 变量未定义
- Windows 8 Javascript 疯狂 - 变量未定义
- $rootScope变量未定义
- 变量未定义
- Javascript:TypeError变量未定义
- 变量未定义/超出范围
- javascript变量未定义
- Javascript变量未定义错误
- $scope变量未定义,尽管它是在$watch函数内部设置的
- 类型错误:变量未定义
- AngularJs $http变量未定义
- JQuery:创建扩展返回变量未定义
- d3.js变量未定义,但在Firefox控制台中,它可以工作
- Javascript 中用于上传表单数据的变量未定义
- 节点.js模块级变量未定义
- Node.js req.session 变量未定义
- 错误:AngularJs 变量未定义,当使用 grunt 运行时.(泽泽尔)
- 将字符串拆分为两个变量 - 未定义不是一个函数