AngularJS – 使用ng-include时丢失范围
发布时间:2020-12-17 09:23:46 所属栏目:安全 来源:网络整理
导读:我有这个模块路由: var mainModule = angular.module('lpConnect',[]). config(['$routeProvider',function ($routeProvider) { $routeProvider. when('/home',{template:'views/home.html',controller:HomeCtrl}). when('/admin',{template:'views/admin.h
我有这个模块路由:
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应该设置。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |