【发布时间】: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.movie= [] this inside homeSearch 和控制器竞赛不一样
-
movieAs.movie如果您更改 this.movie 而不是在此处重新分配 this.movie = response; 将起作用 -
明白了,请问为什么 'this' 转移到变量 - 变量只是一个名字
标签: javascript angularjs angularjs-controlleras