MaterialIn.directive.js
angular.module('mdt')
.directive('mdtMaterialins', materialInsDirective);
function materialInsDirective() {
return {
restrict: 'E',
scope: {
materialoffers: '=',
},
bindToController: true,
templateUrl: 'app/materialOffers/materialIns/materialIns.html',
controllerAs: 'materialInsVm',
controller: MaterialinsController
};
}
function MaterialinsController($scope) {
var vm = this;
console.log(vm); //shows the materialoffers object present
console.log(vm.materialoffers); //show undefined
}
大家好,我做了一个指令,并向它发送了一个对象数组“ materialoffers”,我可以在html页面上访问它,它工作正常,但是idk为什么我不能在控制器内访问它,它显示未定义,任何人都可以告诉我以一种方式可以在控制器内部使用该材质,我想获取其长度并将其存储在这样的$ scope变量中
vm.total=vm.materialoffers.length;
似乎materialoffers
变量可能在$ http调用或类似的操作之后获得其值,因为在创建指令时它不会立即具有值。之所以console.log(vm)
显示该变量,是因为当您单击它时它显示对象的状态(在本例中为vm)。您可以在浏览器控制台中使用以下代码测试此现象:
var object = {};
object; // logs the object in console, shows an empty object
window.setTimeout(function() {
object.testValue = "test value"
}, 10000) // After 10 seconds, the object will get the property testValue
如果您在10秒钟过去之前单击该对象,它将没有该属性,但是在10秒钟之后单击该对象之后,您将在此处看到添加的属性。
尝试用以下代码替换您的控制器代码:
function MaterialinsController($scope) {
var vm = this;
$scope.$watch(function() { return vm.materialoffers }, function() {
console.log(vm.materialoffers)
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句