我有这个模块路线:
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'});
}]);
Run Code Online (Sandbox Code Playgroud)
主页HTML:
<div ng-include src="views.partial1"></div>
Run Code Online (Sandbox Code Playgroud)
partial1
HTML:
<form ng-submit="addLine()">
<input type="text" ng-model="lineText" size="30" placeholder="Type your message here">
</form>
Run Code Online (Sandbox Code Playgroud)
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 = "";
};
...
}
Run Code Online (Sandbox Code Playgroud)
在addLine
函数$scope.lineText
中undefined
,这可以通过添加ng-controller="HomeCtrl"
来解决partial1.html
,但是它会导致控制器被调用两次.我在这里错过了什么?
html javascript angularjs angularjs-scope angularjs-ng-include
我有一个带有隔离范围的指令,它通过引用获取范围变量
angular.module('myApp')
.directive('myDirective', function() {
return {
scope: {
items: '='
},
templateUrl: 'template.html',
replace: true,
controller: 'myDirectiveCtrl',
controllerAs: 'ctrl'
};
})
.controller('myDirectiveCtrl', function($scope) {
this.items = $scope.items;
});
Run Code Online (Sandbox Code Playgroud)
传递方式如下:
<div my-directive items='items'></div>
Run Code Online (Sandbox Code Playgroud)
在外部控制器中,数据被异步加载,并且传递给指令的范围项更新:
angular.module('myApp', [])
.controller('myCtrl', function($scope) {
$scope.setItems = function() {
$scope.items = [
'Here',
'There',
'Everywhere'
];
};
});
Run Code Online (Sandbox Code Playgroud)
加载数据时,我的指令范围之外的范围会更新,但内部则不会
我的HTML:
<div my-directive items='items'></div> <!-- this doesn't update -->
Outside directive
<ul ng-repeat='i in items'> <!-- this does update -->
<li>{{i}}</lu>
</ul>
<button ng-click="setItems()">Set items</button>
Run Code Online (Sandbox Code Playgroud)
如何在我的指令中更新我的范围?我