javascript - How do angularJS instructions expose API to external controllers?
伊谢尔伦
伊谢尔伦 2017-06-17 09:15:37
0
3
1429
<button>expand</button>
<my-menu></my-menu>

I want to click the button to expand menu, but how to expose the API of menu to button?
Or is there any other better way?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
大家讲道理

There is no need to expose any API, you only need to use custom attributes on the instructions for two-way data binding.
For example, if you need to show or hide, you can use a display attribute on the command, for example:

<my-menu display="someCtrlValue" ></my-menu>

Then control this value in your controller, such as

 if(condition){
   $scope.someCtrlValue = true;
 }
 

Of course you only need to specify the two-way binding attributes in the command;

 Module.directive("myMenu", function(){
   return {
       scope: {
           display : '='
       }
       ...
   }
 })
小葫芦

You can specify a two-way binding attribute, such as collapse, when writing the my-menu command, and then use it through <my-menu collapse="val"></my-menu>, and then use the button's The ng-click event can control the val variable (true/false). Then when defining instructions, two-way binding is achieved through independent scopes. Please refer to this article at http://xgfe.github.io/2015/12...

仅有的幸福

Provide a method to perform emit in the instruction, and then the controller performs on monitoring.
In addition, generally the instruction needs to pass the value externally and internally to achieve the flexibility of the instruction. If so, does it not have to modify the instruction every time the API is modified?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template