Tôi có module này các tuyến đường:Mất phạm vi khi sử dụng ng-bao gồm
var mainModule = angular.module('lpConnect', []).
config(['$routeProvider', function ($routeProvider) {
$routeProvider.
when('/home', {template:'views/home.html', controller:HomeCtrl}).
when('/admin', {template:'views/admin.html', controller:AdminCtrl}).
otherwise({redirectTo:'/connect'});
}]);
Home HTML:
<div ng-include src="views.partial1"></div>
partial1
HTML:
<form ng-submit="addLine()">
<input type="text" ng-model="lineText" size="30" placeholder="Type your message here">
</form>
HomeCtrl
:
function HomeCtrl($scope, $location, $window, $http, Common) {
...
$scope.views = {
partial1:"views/partial1.html"
};
$scope.addLine = function() {
$scope.chat.addLine($scope.lineText);
$scope.lines.push({text:$scope.lineText});
$scope.lineText = "";
};
...
}
Trong chức năng addLine
$scope.lineText
là undefined
, điều này có thể được giải quyết bằng cách thêm ng-controller="HomeCtrl"
vào partial1.html
, tuy nhiên nó làm cho bộ điều khiển được gọi hai lần. Tôi đang thiếu gì ở đây?
Cuối cùng tôi hiểu. –
Tôi đã sử dụng ** đối tượng **, nhưng phạm vi của chúng vẫn bị che khuất. Tôi đã thử '$ parent.' và nó hoạt động rất tốt. Tại sao bạn coi nó là một hack? (Tôi có thể thấy nó sẽ bổ sung bảo trì nếu bạn đã cấu trúc lại mã html của mình) – Jess
Cùng một câu hỏi @Jess có, tại sao đây lại bị coi là hack? – qbert65536