我有一个角度控制器,我正在设置使用“控制器为”语法:
angular.module('app', []).controller('ctrl1', ctrl1);
ctrl1.$inject = ['$http', '$compile'];
function ctrl1($http, $compile) {
function someHttpGet() {
//get data
var compiled = $compile(data)($scope);
}
}
我甚至试图将编译数据绑定到范围对象的原因是因为我看到了一个答案,据说这样做,但它对我没有意义,因为在我看来我使用控制器作为语法
如何让它正常工作?
答案 0 :(得分:1)
您没有在控制器中注入$ 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不会自动注入控制器。
答案 1 :(得分:0)
你可以使用类似的东西
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!");
}
然而,我个人尚未使用过这种方法,而且我不确定这是否真的有效。将此视为伪代码。这就是我从阅读文档中收集到的内容。
答案 2 :(得分:0)
angular.module('app', []).controller('Ctrl1' function($scope,$http', '$compile')
{
$scope.someHttpGet = function(){
//get data
var compiled = $compile(data)($scope);
}
});