使用'this'声明的变量var Vm = this"当使用指令访问时,在控制器中不反映值
The variable declared using 'this' or "var Vm = this" in controller is not reflecting the value when accessed using directive
我正在尝试在angular中使用指令。我试图在使用this
或var Vm = this
声明的指令内使用变量值。但是变量的值没有显示出来。
当我尝试在指令中调用变量时。当scope: {}
在指令声明下被调用时,就会发生这种情况。
我附加的控制器和指令的代码:
控制器(js代码)
(function() {
'use strict';
angular
.module('serviceApp')
.controller('ServiceCtrl', ServiceCtrl);
ServiceCtrl.$inject = ['$scope', 'serviceService'];
function ServiceCtrl($scope, serviceService) {
var Vm = this;
Vm.square = function() {
Vm.result = serviceService.square(Vm.number);
};
// $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
// $scope.igor = { name: 'Igor', address: '123 Somewhere' };
}
})();
<<p> 指令代码/strong> (function() {
'use strict';
angular
.module('serviceApp')
.directive('sampleDir', sampleDir);
function sampleDir() {
return {
restrict: 'EA',
scope: {
},
templateUrl: "views/directive-template.html"
};
}
})();
当我从控制器调用html模板下的result变量时,它显示了值。
<div ng-controller="ServiceCtrl as ser">
<label for="num">Enter the number:</label>
<input type="text" id="num" ng-model="ser.number" ng-keyup="ser.square();">
<button>x2</button>
<p>Result: {{ser.result}}</p>
<div>
<sample-dir></sample-dir>
</div>
</div>
指令模板
<p>Square of the number is <strong>{{ser.result}}</strong></p>
当您设置scope: {}
时,它创建一个isolated scope
(这意味着不能访问父作用域)并且其中没有任何项。所以你不能在controller
中访问result
。
在你的情况下,你可以使用scope: true
和你的模板,就像你做的那样,或者改变范围为这个
scope: {
result: '='
};
和模板中的
<sample-dir result='ser.result'></sample-dir>
这将是指令的模板。
<p>Square of the number is <strong>{{result}}</strong></p>
更多信息请参见此处创建自定义指令
这是scope: {}
修改后的示例angular.module('serviceApp', []).controller('ServiceCtrl', ServiceCtrl);
ServiceCtrl.$inject = ['$scope'];
function ServiceCtrl($scope) {
var Vm = this;
Vm.number = 0;
Vm.square = function () {
Vm.result = Vm.number * Vm.number;
};
// $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
// $scope.igor = { name: 'Igor', address: '123 Somewhere' };
}
angular .module('serviceApp').directive('sampleDir', sampleDir);
function sampleDir() {
return {
restrict: 'EA',
scope: {
result: '='
},
template: '<p>Square of the number is <strong>{{result}}</strong></p>'
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='serviceApp' ng-controller="ServiceCtrl as ser">
<label for="num">Enter the number: </label>
<input type="text" id="num" ng-model="ser.number" ng-keyup="ser.square();">
<button>x2</button>
<p>Result: {{ser.result}}</p>
<div>
<sample-dir result='ser.result'></sample-dir>
</div>
这是scope: true
修改后的示例 angular.module('serviceApp', []).controller('ServiceCtrl', ServiceCtrl);
ServiceCtrl.$inject = ['$scope'];
function ServiceCtrl($scope) {
var Vm = this;
Vm.number = 0;
Vm.square = function () {
Vm.result = Vm.number * Vm.number;
};
// $scope.naomi = { name: 'Naomi', address: '1600 Amphitheatre' };
// $scope.igor = { name: 'Igor', address: '123 Somewhere' };
}
angular .module('serviceApp').directive('sampleDir', sampleDir);
function sampleDir() {
return {
restrict: 'EA',
scope: true,
template: '<p>Square of the number is <strong>{{ser.result}}</strong></p>'
};
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='serviceApp' ng-controller="ServiceCtrl as ser">
<label for="num">Enter the number: </label>
<input type="text" id="num" ng-model="ser.number" ng-keyup="ser.square();">
<button>x2</button>
<p>Result: {{ser.result}}</p>
<div>
<sample-dir></sample-dir>
</div>
您需要像这样在作用域中传递结果:
<<p> 指令代码/strong>(function(){
'use strict';
angular
.module('serviceApp')
.directive('sampleDir', sampleDir);
function sampleDir() {
return {
restrict: 'EA',
scope: {
result: "=" // added this line
},
templateUrl: "views/directive-template.html"
};
}
})();
<div ng-controller="ServiceCtrl as ser">
<label for="num">Enter the number: </label>
<input type="text" id="num" ng-model="ser.number" ng-keyup="ser.square();">
<button>x2</button>
<p>Result: {{ser.result}}</p>
</div>
<div>
<sample-dir result="ser.result"></sample-dir> // pass result here
</div>
指令html
<p>Square of the number is <strong>{{result}}</strong></p>
相关文章:
- 如何使用(this)访问Angular 2 http rxjs catch函数中的对象属性
- 为什么我不能在 ajax 成功中访问 $(this)
- 在接受功能上访问燃料UX标语牌中的“this”
- $.each 中的“this”,当需要使用“this”访问对象的函数时
- 在jQuery中,我如何从$.ajax回调函数访问$(this)
- TypeScript:在对象文字字段中的访问器中访问外部“this”
- JavaScript 访问函数构造函数中的“this”
- React Native can'无法在render()方法之外访问this.ops
- 通过函数传递“this”时,我是否可以无法访问“this”的属性
- 如何使用“self = this”访问类属性
- 使用“this”访问对象的值
- arguments属性可以由this访问.some_function.arguments?实际上我无法解释
- 使用let初始化的函数在被this访问时不被替换
- 隐藏匿名JavaScript函数中的变量,但使用“this”访问它们
- 如何使用$(this)访问调用Javascript函数的按钮的id
- 在Javascript中无法通过this访问const变量
- 如何在jQuery中使用$(this)访问子元素?
- 无法使用“this”访问对象.“this”指向“窗口”对象
- 使用jQuery的$(this)访问内部文本
- 使用this访问对象内的对象名称