【问题标题】:angular "controller as syntax" "this" data角度“控制器作为语法”“this”数据
【发布时间】:2016-03-21 06:50:15
【问题描述】:

我不明白我错过了什么

我在 html 上没有得到结果,我认为这个问题用 controllerAs 语法管理

注意:我可以在控制台console.log(this.movie); 中看到结果 - 它来自控制器

app.js

var app = angular.module('mfApp',['ngRoute', 'appControllers']);

app.config(function($routeProvider){
    $routeProvider.
    when('/:detail',{
            templateUrl: 'template/detail.html',
            controller : 'detailCtrl' ,
            controllerAs: 'movieAs'
    }).otherwise({
         redirectTo: '/'
    });
});

controller.js

var mfControllers = angular.module('appControllers', []);


mfControllers.controller('detailCtrl', ['$scope', '$routeParams', 'appServices',  function($scope, $routeParams, appServices){
    this.movie = [];
    this.id = $routeParams.detail;
    appServices.homeSearch(this.id).success(function(response){
        this.movie = response;
        console.log(this.movie);  
        // Yes, I do get array result in console
    });

}]);

html - 模板/detail.html

我的尝试

{{movieAs.Title}} 
{{movieAs.movie.Title}} 
{{movie.movieAs.Title}}  
{{movie.Title}} {{title}}

【问题讨论】:

  • var vm = this; vm.movi​​e= [] this inside homeSearch 和控制器竞赛不一样
  • movieAs.movie 如果您更改 this.movi​​e 而不是在此处重新分配 this.movi​​e = response; 将起作用
  • 明白了,请问为什么 'this' 转移到变量 - 变量只是一个名字
  • What does "this" mean?的可能重复

标签: javascript angularjs angularjs-controlleras


【解决方案1】:
mfControllers.controller('detailCtrl', ['$scope', '$routeParams', 'appServices',  function($scope, $routeParams, appServices){
    var me = this;
    me.movie = [];
    me.id = $routeParams.detail;
    appServices.homeSearch(me.id).success(function(response){
        me.movie = response;
        console.log(me.movie);  
        // Yes, I do get array result in console
    });
}]);

编辑

在 Javascript 中,函数存储为对象,因此从成功内部的 callbeck 方法中,您调用 this,它指的是您正在运行的方法,而不是控制器范围。

最好的做法是将控制器的引用存储在一个可由回调方法访问的变量中。 me 是一个相当随意的名称,但广泛用于引用 parent callerhttps://github.com/johnpapa/angular-styleguide

【讨论】:

  • 明白了,请问为什么 'this' 转移到变量 - 变量只是一个名字
  • @ShibinRagh, this 取决于函数的调用方式,请参阅this post
  • 请再问一个问题 - 我们在“控制器脚本”顶部更改“var vm = this”每个控制器是否是个好方法
  • 是的,这样做是个好习惯。只能使用 vm 而不是这个。
  • 一个好的建议是使用 vm 语法,正如指南所说的 github.com/johnpapa/angular-styleguide
【解决方案2】:

问题是由于错误的this 引用。

var mfControllers = angular.module('appControllers', []);

mfControllers
  .controller('detailCtrl', ['$scope', '$routeParams', 'appServices', function($scope, $routeParams, appServices) {
    var vm = this;

    vm.movie = [];
    vm.id = $routeParams.detail;
    appServices
      .homeSearch(vm.id)
      .success(function(response) {
        // vm !== this; here
        vm.movie = response;
        console.log(vm.movie);  
    });
  }]);

使用controllerAs 语法的一个好习惯是在控制器的最开始将this 分配给vm。它正确地保存了控制器的引用。

这是必要的,因为 javascript 函数范围。在这里解释它会很长,但它的要点是 function 创建了一个新范围,thisfunction 中会有所不同。托德莫特对此有一个非常great write up

【讨论】:

  • 请再问一个问题 - 我们在“控制器脚本”顶部更改“var vm = this”每个控制器是不是很好
  • @ShibinRagh 是的。它有两个目的:1)它解决了this 问题,2)很明显vm 指的是控制器。顺便说一句,vm 的意思是“视图模型”,这就是控制器的真正含义
猜你喜欢
  • 2016-11-13
  • 1970-01-01
  • 2016-03-11
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-04
  • 1970-01-01
相关资源
最近更新 更多