我有这个模块路由:
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>
首页控件:
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是未定义的,这可以通过添加ng-controller =“HomeCtrl”到partial1.html来解决,但是它会导致控制器被调用两次,我在这里缺少什么?
这是因为ng-include创建了一个新的子范围,所以$ scope.lineText不会改变。我认为这指的是当前的范围,所以this.lineText应该设置。
原文链接:https://www.f2er.com/angularjs/147536.html