I’m building an an owa for OpenMRS. I’m using angular 1.5 with components. My project was scaffolded with the openmrs-owa generator.
My component controllers work in some instances and don’t in others. Say I have a component called helloworld-component if you have used the openmrs-owa generator, you’ll know that this means I have a
- helloWorld.component.js
- helloWorld.controller.js
- helloWorld.html
helloWorld.component.js
import template from './helloWorld.html';
import controller from './helloWorld.controller';
let helloWorldComponent = {
restrict: 'E',
bindings: {},
template: template,
controller: controller,
controllerAs: 'vm'
};
export default helloWorldComponent;
helloWorld.controller.js
class HelloWorldController {
constructor() {
var vm = this;
vm.buttonClicked = () => {
console.log("Hello World!!!");
}
}
}
export default HelloWorldController;
helloWorld.html
<button ng-click="vm.buttonClicked()">Click Me!!!</button>
inside home.js I have included the component using (a lot of code is excluded)
...
import helloWorldComponent from './components/hello-world/helloWorld.component;
...
.component('helloWorld', helloWorldComponent);
And when I use my component in the
<hello-world></hello-world>
And this works as expected. I see my button that says Click Me!!! and when I click it, it’s logged to the console that Hello World!!!
But if I were to navigate to this component using state change programmatically, clicking on the button won’t work. Let’s say for instance I have a state configuration like
$stateProvider.state('helloWorld', {
url: '/helloWorld',
template: require('./components/hello-world/helloWorld.html'),
});
If I navigate to this state by changing programmatically from another components controller or by clicking an ancore tag that has an ui-sref="helloWorld"
I still get to see my button that says Click Me!!! but clicking has not effect(that is my controller method is not fired up).
FYI, I’ve not worked with this(Angular 1.x) before. I’ve only used components in Angular 2/4 with typescript. I basically proceed by looking at examples of a sample app generated by the openmrs-owa but there’s nothing in there to help me with this now. I understand JavaScript syntax and have worked with components in Angular 4 so just reading example codebase has been moving on fine until now. cc @raff @pascal