Comments (5)
ah i think i misunderstood your question. To rephrase, you have a child component that is responsible for taking a picture that needs to be triggered by the parent component. Is this correct? If so, one way of doing it is the following:
camera.component.js
class CameraController {
constructor(CameraService) {
this._cameraService = CameraService;
}
$onChanges(changes) {
if (changes.buttonPressed.currentValue) {
this._takePicture();
}
}
_takePicture() {
this._cameraService
.takePicture()
.then(() => /* some code */)
.catch(err => /* handle err */)
.finally(() => this.resetButton());
}
}
export default Camera {
template: require('./camera.html'),
controller: CameraController,
controllerAs: 'vm',
bindings: {
buttonPressed: '<',
resetButton: '&'
}
};
parent.component.js
class ParentController {
takePicture() {
this.buttonPressed = true;
}
resetButton() {
this.buttonPressed = false;
}
}
export default Parent {
template: require('./parent.html'),
controller: ParentController,
controllerAs: 'vm'
};
parent.html
<div>
<camera-component
button-pressed="vm.buttonPressed"
reset-button="vm.resetButton()"
/>
<button ng-click="vm.takePicture()">Say Cheeze</button>
</div>
also - confused on why the trigger can't exist within the child component
from angularjs-styleguide.
I have the same issue with a child component
from angularjs-styleguide.
I'm a little confused on the goal there, but you could leverage one-way bindings to traverse data into the child component, without mutating the parent scope.
from angularjs-styleguide.
can you give me an example how to solve it using one-way bindings?
from angularjs-styleguide.
In order to decouple the parent from child further, would this approach be considered in good style?
class CameraController {
constructor(CameraService) {
this._cameraService = CameraService;
}
$onChanges(changes) {
if (changes.buttonPressed.currentValue) {
this._takePicture();
}
}
_takePicture() {
this._cameraService
.takePicture()
.then(() => /* some code */)
.catch(err => /* handle err */);
}
}
export default Camera {
template: require('./camera.html'),
controller: CameraController,
controllerAs: 'vm',
bindings: {
buttonPressed: '<',
}
};
parent.component.js
class ParentController {
constructor($timeout) {
this.$timeout = $timeout;
this._buttonPressed = false;
}
get buttonPressed() {
return _buttonPressed;
}
set buttonPressed(value) {
const { $timeout } = this;
$timeout(() => {
this._buttonPressed = value;
if (this._buttonPressed) {
$timeout(() => {
this._buttonPressed = false;
});
}
});
}
takePicture() {
this.buttonPressed = true;
}
}
ParentController.$inject = ['$timeout'];
export default Parent {
template: require('./parent.html'),
controller: ParentController,
controllerAs: 'vm'
};
parent.html
<div>
<camera-component button-pressed="vm.buttonPressed" />
<button ng-click="vm.takePicture()">Say Cheeze</button>
</div>
Ultimately, we leave the resetting of the buttonPressed property to the parent controller. This still triggers the change in the child and now the child does not care about manipulating the state of its parent.
from angularjs-styleguide.
Related Issues (20)
- Yeoman generator for this style guide HOT 3
- Somekind of a new syntax with ng1.6 controllers.
- feature request: Advice on using Interface
- Missign injectable in directive with Classes
- Life without factories HOT 1
- Granularity of Modules HOT 1
- class constructors must be invoked with |new| HOT 1
- Wrong example in directive
- ng-annotate HOT 4
- index.js (index.ts) usage
- syntax error(s) in Typescript directive section for TodoAutoFocus as TS Class
- Register service with static constructor function? HOT 1
- calendar issues with table cell width with title column header
- Translating README to Spanish HOT 4
- Typescript Version: Which @types/angular version was used?
- reuse filters across components HOT 1
- Can't access form object in component's controller HOT 1
- Circular dependencies
- Spanish translation is old and don't match with current code.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angularjs-styleguide.