gpt4 book ai didi

angularjs - 如何在 AngularJs 1.6 组件的 Controller 中使用绑定(bind)

转载 作者:行者123 更新时间:2023-12-01 22:12:47 25 4
gpt4 key购买 nike

你好,这是我的组件:

angular.module('myApp').component('dendroCtrl', {
templateUrl: '/templates/dendro.html',
bindings: {
id: '=',
type: '=',
mini: "="
},
controller: function ($scope, Api) {
//Dendro

var test = mini;

我试过这个:

var test = mini; 
var test = this.mini;
var test = $scope.mini;

Mini 每次都是未定义的。

如何在我的 Controller 中使用我的绑定(bind)?谢谢

编辑

这就是我调用组件的方式:

  <dendro-ctrl id="149" type="Demand" mini="false"></dendro-ctrl>

最佳答案

来自 the documentation :

Components have a well-defined lifecycle Each component can implement "lifecycle hooks". These are methods that will be called at certain points in the life of the component. The following hook methods can be implemented:

  • $onInit() - Called on each controller after all the controllers on an element have been constructed and had their bindings initialized (and before the pre & post linking functions for the directives on this element). This is a good place to put initialization code for your controller.
  • $onChanges(changesObj) - Called whenever one-way bindings are updated. The changesObj is a hash whose keys are the names of the bound properties that have changed, and the values are an object of the form

所以你可以使用:

controller: function(Api) {
var ctrl = this;
ctrl.$onInit = function() {
console.log(ctrl.mini);
}
}

如果要显示迷你绑定(bind)的初始值。

关于angularjs - 如何在 AngularJs 1.6 组件的 Controller 中使用绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46874031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com