我有一个角控制器,我要设置它使用"controller as"语法:
angular.module('app', []).controller('ctrl1', ctrl1);
ctrl1.$inject = ['$http', '$compile'];
function ctrl1($http, $compile) {
function someHttpGet() {
//get data
var compiled = $compile(data)($scope);
}
}
我甚至试图将编译数据绑定到作用域对象的原因是因为我看到了一个答案,说要这样做,但这对我来说没有意义,因为在我的视图中,我使用控制器作为语法。
我怎样才能使它正确工作?
你没有在控制器中注入$作用域:
ctrl1.$inject = ['$http', '$compile', '$scope'];//like others $scope needs to be injected
function ctrl1($http, $compile, $scope) {
function someHttpGet() {
//get data
var compiled = $compile(data)($scope);
}
}
$scope不会自动注入到控制器中。
您可能会使用这样的内容
angular.module('app', [])
.controller('myController', ['$http', Controller1]);
function Controller1($http) {
// you can use 'this' here instead of using $scope
this.name = "foobar";
var that = this;
// using $http then
$http.get("/your/api").success(data) {
that.data = data;
});
}
// This is a $scope function then
Controller1.prototype.sayHello = function() {
console.log("Hello World!");
}
然而,我个人还没有使用过这种方法,我不确定这是否有效。将此视为伪代码。
angular.module('app', []).controller('Ctrl1' function($scope,$http', '$compile')
{
$scope.someHttpGet = function(){
//get data
var compiled = $compile(data)($scope);
}
});