Javascript angularjs无法在链接函数内分配范围变量
嗨,我有下面的代码片段。我的目标是允许创建多个div,并将它们的位置、文本、高度和宽度绑定到控制器的模型Javascript angularjs无法在链接函数内分配范围变量,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,嗨,我有下面的代码片段。我的目标是允许创建多个div,并将它们的位置、文本、高度和宽度绑定到控制器的模型 appModule.controller('textController', function($scope){ var box = {x: '0', y: '0', height: '100px', width: '200px', text: 'this is the default text'}; $scope.textBoxes = [box]; $scop
appModule.controller('textController', function($scope){
var box = {x: '0', y: '0', height: '100px', width: '200px', text: 'this is the default text'};
$scope.textBoxes = [box];
$scope.addNewTextBox = function(){
console.log("creating new textbox!!!");
$scope.textBoxes.push(box);
};
});
appModule.directive('tbox', function($document){
return {
restrict: "E",
template: '<div><div ng-transclude></div>',
transclude: true,
scope: {
model: "=",
},
link: function postLink(scope, element, attrs, ctrl){ //the scope here is the object's scope
var length = scope.$parent.textBoxes.length;
console.log(length);
scope.model = scope.$parent.textBoxes[length-1];
}
}
});
appModule.controller('textController',函数($scope){
变量框={x:'0',y:'0',高度:'100px',宽度:'200px',文本:'这是默认文本'};
$scope.textboxs=[box];
$scope.addNewTextBox=函数(){
log(“创建新文本框!!!”;
$scope.textboxs.push(box);
};
});
指令('tbox',函数($document){
返回{
限制:“E”,
模板:“”,
是的,
范围:{
型号:“=”,
},
link:函数postLink(scope,element,attrs,ctrl){//这里的作用域是对象的作用域
var length=范围$parent.textboxs.length;
控制台日志(长度);
scope.model=scope.$parent.textboxs[length-1];
}
}
});
我在这里遇到了一些问题。我无法将作用域的模型变量分配给控制器中的textboxs变量,原因是
Error: Non-assignable model expression: undefined (directive: tbox)
at Error (<anonymous>)
at h (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:43:213)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:43:326)
at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:87:13)
at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:89:198)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:16:239
at Object.d [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.
错误:不可赋值模型表达式:未定义(指令:tbox)
错误()
在h(https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:43:213)
反对。(https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:43:326)
at Object.e.$digest(https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:87:13)
在Object.e.$apply(https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:89:198)
在https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js:16:239
在Object.d[作为调用](https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.
知道原因吗?由于错误报告“model”未定义,请尝试从指令的隔离范围中删除
model:“=”
所以看起来是这样的:
scope: {},
在父作用域上有一个名为
model
的对象吗?我想知道它是否在scope
属性的绑定上遇到了障碍。@KayakDave,没有,我需要吗?很抱歉,我对隔离作用域的概念仍然有点模糊。我根据这个想法进行了修改。好吧,你在作用域中的“=”是有问题的我想绑定到一个名为model
的对象,因为你没有在等号的右边指定任何东西。你需要=
吗?嗯,我实际上解释为当拖动对象时,该对象的x,y发生变化时,控制器的模型也会发生变化。这是我的目标。这符合条件吗?是的,看着你的代码,我看到你的o有一个存储x和y的模型对象。是的,看起来不错。plnkr似乎在为我工作-你解决问题了吗?