使用ng-include时失去作用域
我有这个模块的路线:
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'});
}]);
原始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 = "";
};
...
}
在addLine
功能$scope.lineText
的undefined
,这可以通过添加解决ng-
controller="HomeCtrl"到partial1.html
,但它会导致控制器被调用两次。我在这里想念什么?
回答:
这是因为ng-
include它会创建一个新的子范围,因此$scope.lineText
不会更改。我认为这this
是指当前范围,因此this.lineText
应设置。
以上是 使用ng-include时失去作用域 的全部内容, 来源链接: utcz.com/qa/410947.html