使用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,但是它会导致控制器被调用两次。我在这里错过了什么?
3 回答
莫回无
TA贡献1865条经验 获得超7个赞
这是因为ng-include
它创建了一个新的子范围,因此$scope.lineText
不会更改。我认为这this
是指当前的范围,所以this.lineText
应该设置。
添加回答
举报
0/150
提交
取消