我在1.6.x中编写angularjs应用...并且我在component()上遇到问题,其中$ onInit()看不到任何已定义的var ...

每个变量都未定义

这是我的代码。

angular.module('carFilter', []).
        component('carFilter', {
            controller: function (carFactory, $http) {
                this.service = carFactory;
                this.updatedObj = {};
//when i put breakpoint here on chrome, i can see the factory obj.. but..
//when it goes to oninit, init can not see this obj



            this.$onInit = function () {
                    this.testing = 'a';
                    console.log(this.service); //th

                    loadFilter().then(function (result) {

                    updateFilter(result)
                });
            };

            function loadFilter() {
               //$http here to return promise
            }
            function updateFilter(responseData) {
               //using response data to update i wanted to update the this.updatedObj
               this.updatedObj = responseData;
//but here, when this function is triggered, every object is undefined...
//I tried to put this.sercie and this.updatedObj in $onInit(), but it was still getting UNDEINFED....
//it could not see this.testing either...
//how can i update object in the controller??
            }
       },
       templateUrl: 'someURL'
});


先感谢您...

最佳答案

现在,当您输入$ onInit函数(){}时,它指的是$ onInit,因此成为一个问题,即允许传入来自控制器的变量。

我知道很多人根据您从控制器的第一行中学到的内容来声明vm或ctrl。



var ctrl = this;
ctrl.service = carFactory;

ctrl.$onInit = function() {
    console.log(ctrl.service);
}

关于javascript - 组件$ onInit()在 Controller 中看不到变量-angularjs 1.6.x,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42792942/

10-12 15:52