Answer the question
In order to leave comments, you need to log in
AngularJS how to set a getter/setter for a component controller in es5?
I saw in an article on habré how not to use $scope in components there is an example of code, but it is on es6, how to do the same on es5?
class MyComponent {
get bindedValue() {
return this.$value;
}
set bindedValue(value) {
this.$value = value;
// а теперь вызовем метод, который должен обновить что-то
// вместо того, что бы вешать неявный ватчер
this.makeSomethingWithNewValue();
}
}
angular.module('app').component('myComponent', {
bindings: {
"bindedValue": "="
},
template: `<div>Какой-то шаблон</div>`,
controller: MyComponent
});
dialogFormCtrl.prototype.showDialogForm = function () {};
dialogFormCtrl.prototype.model = {};
(function (angular) {
'use strict';
angular.module('FooBarBaz')
.component('dialogForm', {
bindings : {
showForm : '=',
idTest : '=',
callbackFn: '&',
},
template : template,
controller: dialogFormCtrl
});
function template() {
return `
*
*
*
`;
}
dialogFormCtrl.$inject = ['testsServiceOne', '$mdDialog'];
function dialogFormCtrl(testsServiceOne, $mdDialog) {
var vm = this;
vm.showDialogForm = showDialogForm;
vm.offDialog = offDialog;
vm.addConstant = addConstant;
vm.deleteConstant = deleteConstant;
vm.saveFormDialog = saveFormDialog;
vm.model = {
showForm: false,
loadTest: true,
test : {}
};
activate();
////////////////////////////////////////////////
function activate() {
}
function showDialogForm(value) {
/**
*
*/
}
function offDialog() {
$mdDialog.cancel();
}
function getTestJson(id) {
return testsServiceOne.getTestForm(id);
}
function addConstant(server) {
/**
*
*/
}
function deleteConstant(index, server) {
/**
*
*/
}
function getDateTimeShtamp(time) {
return testsServiceOne.getDateTimeShtamp(time);
}
function saveFormDialog() {
/**
*
*/
}
}
/**
* Тут полная дичь, но она работает.
* Как верно указать назначить геттер/сеттер ?
*
*/
dialogFormCtrl.prototype = {
constructor: dialogFormCtrl,
get showForm() {
return this.model.showForm;
},
set showForm(value) {
this.model.showForm = value;
this.showDialogForm(value);
},
};
/**
* Именно нижние две строчки у меня вызывают непонимание, если их не указать то все падает, так как не находит функцию "showDialogForm"
*/
dialogFormCtrl.prototype.showDialogForm = function () {};
dialogFormCtrl.prototype.model = {};
})(window.angular);
Answer the question
In order to leave comments, you need to log in
ES5 class example:
function MyComponent() {
this._bindedValue = 'someValue';
}
MyComponent.prototype.getBindedValue = function() {
return this._bindedValue;
}
MyComponent.prototype.setBindedValue = function(value) {
this._bindedValue = value;
this.makeSomethingWithNewValue();
}
var MyComponent() {
_bindedValue: 'someValue',
get bindedValue() {
return this._bindedValue;
}
set bindedValue(value) {
this._bindedValue = value;
this.makeSomethingWithNewValue();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question