Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了AngularJS – 使用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>

首页控件:

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应该设置。

大佬总结

以上是大佬教程为你收集整理的AngularJS – 使用ng-include时丢失范围全部内容,希望文章能够帮你解决AngularJS – 使用ng-include时丢失范围所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。